-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbufflog.ts
144 lines (127 loc) · 3.57 KB
/
bufflog.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
REQ_KEYS_REDACTED,
REQ_CONTEXT_KEYS_REDACTED,
RES_KEYS_REDACTED,
RES_CONTEXT_KEYS_REDACTED
} from './constants'
const pinoLogger = require('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',
// Define "base" fields
// soon: remove the `v` field https://github.com/pinojs/pino/issues/620
base: {},
// 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 ]',
},
});
import redact from 'redact-object'
export const KEYS_TO_REDACT = [
'__dd_span',
'_datadog',
'access_token',
'access-token',
'accessToken',
'publishAccessToken',
'access_token_secret',
'access-token-secret',
'accessTokenSecret',
'appsecret_proof',
'appsecret_time',
'authorization',
'buffer_session',
'bufferapp_ci_session',
'codeVerifier',
'cookie',
'credentials',
'input_token',
'password',
'rawHeaders',
'refresh_token',
'refresh-token',
'refreshToken',
'secret',
'shared_access_token',
'shared-access-token',
'sharedAccessToken',
'x-buffer-authentication-access-token',
'x-buffer-authentication-jwt',
'x-buffer-authorization-jwt',
]
export function getLogger() {
return pinoLogger;
}
function sanitizeContext(context?: object): object | undefined {
// For now, to keep the change limited, disabling this
return context
// Will re-enable this after Campsite decision
// if (!context) {
// return
// }
//
// return redact(context, KEYS_TO_REDACT, '[ REDACTED ]', {
// ignoreUnknown: true,
// })
}
export function debug(message: string, context?: object) {
pinoLogger.debug({context: sanitizeContext(context)}, message);
}
export function info(message: string, context?: object) {
pinoLogger.info({context: sanitizeContext(context)}, message);
}
export function notice(message: string, context?: object) {
pinoLogger.notice({context: sanitizeContext(context)}, message);
}
export function warning(message: string, context?: object) {
pinoLogger.warn({context: sanitizeContext(context)}, message);
}
export function error(message: string, context?: object) {
pinoLogger.error({context: sanitizeContext(context)}, message);
}
// for consistency with php-bufflog, critical == fatal
export function critical(message: string, context?: object) {
pinoLogger.fatal({context: sanitizeContext(context)}, message);
}
export function middleware() {
return require('pino-http')({
logger: pinoLogger,
// Define a custom logger level
customLogLevel: function (res: any, err: any) {
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