-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.js
57 lines (45 loc) · 1.31 KB
/
context.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
function ContextLogger(logger, name) {
if (!logger) throw new Error();
this.logger = logger;
this.name = name || 'undefined-context';
}
const proto = ContextLogger.prototype;
proto._format = function(level, line) {
return this.logger.format(this.name, level, line);
};
['info', 'warn', 'debug'].forEach((level) => {
proto[level] = function(line) {
this.logger.write(this._format(level.toUpperCase(), line));
this.logger.emit(level, line);
}
});
proto._handleError = function(level, line, error) {
// Called with message and error
if (error) {
this.logger.writeError(this._format(level, line));
this.stack(error);
return;
}
// Called with error only
if (line instanceof Error) {
this.logger.writeError(this._format(level, line.message));
this.stack(line);
return;
}
// Called with message
this.logger.writeError(this._format(level, line));
};
proto.error = function(line, error) {
this._handleError('ERROR', line, error);
this.logger.emit('err', line, error);
};
proto.fatal = function(line, error) {
this._handleError('FATAL', line, error);
this.logger.emit('fatal', line, error);
};
proto.stack = function(error) {
this.logger.writeError(this._format('STACK', error.stack));
this.logger.emit('stack', error);
};
module.exports = ContextLogger;