-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog.js
More file actions
91 lines (71 loc) · 2.21 KB
/
Copy pathlog.js
File metadata and controls
91 lines (71 loc) · 2.21 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
const { relayEvents, Relayer } = require('./relay.js');
class Logger extends Relayer {
constructor (logOptions, relayOptions) {
super(relayOptions);
if (logOptions) {
if (Array.isArray(logOptions)) {
logOptions = {
to: logOptions
};
}
if (typeof logOptions.log === 'function') {
this.out = logOptions;
}
else {
let to = logOptions.to;
if (to) {
this.to = to;
this.out = {
log: (event, ...args) => {
let s = args.map(this.format).join(', ');
to.push(`${event}${s ? ': ' : ''}${s}`);
}
};
if (typeof logOptions.format === 'function') {
this.format = logOptions.format.bind(logOptions);
}
}
}
if (logOptions.level) {
this.level = logOptions.level;
}
if ('mask' in logOptions) {
let m = logOptions.mask;
if (typeof m === 'number') {
m = { '*': m };
}
this.mask = m;
}
if (logOptions.prefix) {
this.prefix = logOptions.prefix;
}
}
}
format (arg) {
return (typeof arg === 'string') ? JSON.stringify(arg) : String(arg);
}
doRelay (event, args) {
let a = args;
let m = this.mask;
let level = this.level[event] || 'log';
m = m[event] || m['*'];
if (m != null) {
a = args.filter((val, i) => m & (1 << i));
}
this.out[level](`${this.prefix}${event}`, ...a);
}
}
Object.assign(Logger.prototype, {
isEventLogger: true,
level: {},
mask: {},
out: console,
prefix: ''
});
function logEvents (watchable, logOptions, relayOptions) {
return relayEvents(watchable, new Logger(logOptions, relayOptions));
}
logEvents.Logger = Logger;
logEvents.logEvents = logEvents;
module.exports = logEvents;