-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.ts
72 lines (62 loc) · 1.55 KB
/
logger.ts
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
import bunyan from 'bunyan';
import bunyanFormat from 'bunyan-format';
const outputMode = process.env.LOG_OUTPUT_MODE || 'bunyan';
const formatOut = bunyanFormat({
outputMode,
levelInString: true,
colorFromLevel: {
10: 'white', // TRACE
20: 'yellow', // DEBUG
30: 'green', // INFO
40: 'magenta', // WARN
50: 'red', // ERROR
60: 'inverse', // FATAL
},
});
const lvlMap = {
FATAL: bunyan.FATAL,
ERROR: bunyan.ERROR,
WARN: bunyan.WARN,
INFO: bunyan.INFO,
DEBUG: bunyan.DEBUG,
TRACE: bunyan.TRACE,
};
const appName = process.env.APP_NAME || 'app';
const defaultLevel = 'INFO';
const level = process.env.LOG_LEVEL || defaultLevel;
function errSerializerWithErrors(err) {
const output = bunyan.stdSerializers.err(err);
if (err.errors) {
output.errors = err.errors;
}
return output;
}
function requestSerializer(req) {
if (!req || !req.connection) {
return req;
}
return {
method: req.method,
url: req.url,
headers: req.headers,
remoteAddress: req.connection.remoteAddress,
remotePort: req.connection.remotePort,
body: req.body,
};
}
const serializers = Object.create(bunyan.stdSerializers);
serializers.err = errSerializerWithErrors;
serializers.req = requestSerializer;
const loggers = {};
export function getLogger(loggerName = 'defaultLogger') {
if (!loggers[loggerName]) {
loggers[loggerName] = bunyan.createLogger({
serializers,
level: lvlMap[level.toUpperCase()],
name: appName,
src: true,
stream: formatOut,
});
}
return loggers[loggerName];
}