-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (137 loc) · 4.7 KB
/
index.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
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
145
146
147
148
149
150
151
152
153
154
155
156
const path = require('node:path');
const { AsyncLocalStorage } = require('node:async_hooks');
const pino = require('pino');
const stringify = require('safe-stable-stringify');
module.exports = {
init
}
function init(options = { machine: true }) {
// https://github.com/pinojs/pino-pretty?tab=readme-ov-file#usage-with-jest
const sync = options?.sync || Boolean(options?.test) || false;
const maxSize = options?.maxSize || 10000;
const als = options?.als || new AsyncLocalStorage();
function configureMachineLogger() {
return (options?.machine) ? [{
target: 'pino/file',
level: options?.machine?.level || 'info',
options: {
destination: options?.machine?.destination || 1,
sync,
}
}] : [];
}
function configureHumanLogger() {
return (options?.human) ? [{
target: 'pino-pretty',
level: options?.human?.level || 'info',
options: {
destination: options?.human?.destination || 1,
ignore: 'time,severity',
sync,
}
}] : [];
}
function configureTestLogger() {
return (options?.test) ? [{
target: path.resolve(path.join('lib', 'post-message-transport')),
level: options?.test?.level || 'info',
}] : [];
};
const formatters = {
level(label, number) {
return { level: number, severity: label }
},
log(object) {
const { error, ...context } = object;
return { err: error, ...context }
},
}
const hooks = {
/*
Defines a custom logMethod that:
1. Exposes a conventional logging API, i.e. logger.info(message, [context])
2. Merges the asynchronous local storage store and the context, prefering the latter
3. Reports oversized messages
*/
logMethod(inputArgs, method, level) {
const [message, ctx] = getArgs(inputArgs);
const store = als.getStore() || {};
const outputArgs = [{ ...store, ...ctx }, message ];
const size = Buffer.byteLength(stringify(outputArgs));
return size > maxSize
? method.apply(this, [fail(`Log record size of ${(size).toLocaleString()} bytes exceeds maximum of ${(maxSize).toLocaleString()} bytes`)])
: method.apply(this, outputArgs);
}
}
const targets = [].concat(
configureMachineLogger(),
configureHumanLogger(),
configureTestLogger(),
)
const transport = pino.transport({
targets: targets
});
const logger = pino({
base: null,
formatters,
hooks,
nestedKey: 'ctx',
redact: {
// pino's redaction library is severely limited :(
// https://github.com/davidmarkclements/fast-redact/issues/5
paths: [
'password',
'email',
'*.password',
'*.email',
'*[*].password',
'*[*].email',
'req.headers.*',
'request.headers.*',
'res.headers.*',
'resp.headers.*',
'response.headers.*',
'res.req.headers.*',
'resp.req.headers.*',
'response.request.headers.*',
'err.req.headers.*',
'err.request.headers.*',
'err.res.headers.*',
'err.resp.headers.*',
'err.response.headers.*',
'err.res.req.headers.*',
'err.resp.req.headers.*',
'err.response.request.headers.*',
].concat(options?.redact?.paths || []),
}
}, transport);
transport.on('message', (...args) => {
logger.emit('message', ...args);
})
module.exports.logger = logger;
module.exports.als = als;
return logger;
}
function getArgs(inputArgs) {
let args;
if (inputArgs.length >= 2 && inputArgs[1] instanceof Date) args = [inputArgs[0], { ts: inputArgs[1] }];
else if (inputArgs.length >= 2 && inputArgs[1] instanceof Error) args = [inputArgs[0], { err: inputArgs[1] }];
else if (inputArgs.length >= 2) args = inputArgs.slice(0, 2);
else if (inputArgs.length === 1 && ['boolean', 'number', 'string'].includes(typeof inputArgs[0])) args = inputArgs;
else if (inputArgs.length === 1 && typeof inputArgs[0] === 'bigint') args = [String(inputArgs[0])];
else if (inputArgs.length === 1 && inputArgs[0] instanceof Date) args = [undefined, { ts: inputArgs[0] }];
else if (inputArgs.length === 1 && inputArgs[0] instanceof Error) args = [undefined, { err: inputArgs[0] }];
else if (inputArgs.length === 1) args = [undefined].concat(inputArgs);
else args = inputArgs;
return args.some(isNotEmpty) ? args : [undefined, fail('Empty log message')];
}
function isNotEmpty(value) {
if (value === undefined) return false;
if (value === null) return false;
if (typeof value === 'string' && value.trim().length === 0) return false;
if (typeof value === 'object' && Object.keys(value).length === 0) return false;
return true;
}
function fail(message) {
return { err: new Error(message) }
}