-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
31 lines (23 loc) · 820 Bytes
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const ContextLogger = require('./context');
function Logger() {
this.host = process.env.HOSTNAME || 'undefined-hostname';
this.service = process.env.SERVICE_NAME || 'undefined-service';
this.format = function format(context, level, line) {
const now = (new Date()).toISOString();
const host = this.host;
const service = this.service;
return `${now} ${service} ${host} ${level} ${context} ${line}`;
};
this.write = console.log;
this.writeError = console.error;
EventEmitter.call(this);
}
util.inherits(Logger, EventEmitter);
const proto = Logger.prototype;
proto.for = function createContextLogger(context) {
return new ContextLogger(this, context);
};
module.exports = new Logger();