-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
95 lines (90 loc) · 2.37 KB
/
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
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
92
93
94
95
const {createLogger, format, transports, Transport} = require('winston');
const {align, colorize, combine, label, metadata, printf, timestamp} = format;
function Logger(fileName, customMetadata = {}){
if (fileName === undefined) {
throw new Error('Logger requires the file name of where it\'s being used.');
}
if(!new.target){
return new Logger(...arguments);
}
// TODO fix this ugly hack
if(Logger.instance === undefined && typeof fileName === 'object'){
const config = fileName;
const types = {};
const stdFormat = combine(
// metadata(),
// metadata({ fillWith: ['fileName'].concat(Object.keys(customMetadata)) }),
metadata({ fillWith: Object.keys(customMetadata) }),
timestamp({format: 'YY-M-D hh:mm:ss:SSS'}),
// label({label: }),
// colorize(),
align(),
printf(info => `${info.timestamp} [${info.fileName}] ${info.level}: ${info.message}`)
);
Object.defineProperties(Logger, {
addTransport: {
value: (name, newTransport) => {
if (newTransport instanceof Transport && types[name] === undefined) {
Object.defineProperty(types, name, {
configurable: true,
enumerable: true,
value: () => newTransport
});
}
Logger.instance.add(types[name]());
}
},
getLogLevel: {
value: () => Logger.instance.level
},
instance: {
value: createLogger({
level: config.logLevel,
transports: [new transports.File({
filename: config.logFile,
format: stdFormat,
maxsize: '5242880',
zippedArchive: true
}), new transports.Console({
format: stdFormat
})]
})
},
removeTransport: {
value: (transportToRemove) => {
if (typeof transportToRemove === 'string') {
Logger.instance.remove(types[transportToRemove]());
delete types[transportToRemove];
} else {
Logger.instance.remove(transportToRemove);
// TODO: find and delete when no name given
}
}
},
setLogLevel: {
value: (newLevel) => {
Logger.instance.level = newLevel;
}
},
transportTypes: {
get: () => types
}
});
}
const logger = Logger.instance.child({
fileName,
...customMetadata
});
Object.defineProperties(logger, {
getLogLevel: {
value: () => Logger.instance.level
},
setLogLevel: {
value: (newLevel) => {
Logger.instance.level = newLevel;
}
}
});
return logger;
}
module.exports = Logger;