-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbufflog.ts
More file actions
95 lines (81 loc) · 2.24 KB
/
bufflog.ts
File metadata and controls
95 lines (81 loc) · 2.24 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
92
93
94
95
import pino from 'pino'
import pinoHttp from 'pino-http'
import {
REQ_KEYS_REDACTED,
REQ_CONTEXT_KEYS_REDACTED,
RES_KEYS_REDACTED,
RES_CONTEXT_KEYS_REDACTED
} from './constants'
const pinoLogger = pino({
level: process.env.LOG_LEVEL ? String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) : "notice",
// probably we want to call it `msg`. if so, let's change the PHP library instead
messageKey: 'message',
// notice doesn't exist in pino, let's add it
customLevels: {
debug: 100,
info: 200,
notice: 250,
warn: 300,
error: 400,
fatal: 500
},
// necessary if we want to override the level "number"
useOnlyCustomLevels: true,
redact: {
paths: [
...REQ_KEYS_REDACTED,
...RES_KEYS_REDACTED,
...REQ_CONTEXT_KEYS_REDACTED,
...RES_CONTEXT_KEYS_REDACTED,
],
censor: '[ REDACTED ]',
},
});
export function getLogger() {
return pinoLogger;
}
export function debug(message: string, context?: object) {
pinoLogger.debug({context}, message);
}
export function info(message: string, context?: object) {
pinoLogger.info({context}, message);
}
export function notice(message: string, context?: object) {
pinoLogger.notice({context}, message);
}
export function warning(message: string, context?: object) {
pinoLogger.warn({context}, message);
}
export function error(message: string, context?: object) {
pinoLogger.error({context}, message);
}
// for consistency with php-bufflog, critical == fatal
export function critical(message: string, context?: object) {
pinoLogger.fatal({context}, message);
}
export function middleware() {
return pinoHttp({
logger: pinoLogger,
// Define a custom logger level
customLogLevel: function (_req, res, err) {
if (res.statusCode >= 400 && res.statusCode < 500) {
// for now, we don't want notice notification on the 4xx
return 'info'
} else if (res.statusCode >= 500 || err) {
return 'error'
}
return 'info'
},
})
}
const BuffLog = {
getLogger,
debug,
info,
notice,
warning,
error,
critical,
middleware,
}
export default BuffLog