-
Notifications
You must be signed in to change notification settings - Fork 21
/
logging.js
74 lines (69 loc) · 2.55 KB
/
logging.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
const tracer = require('dd-trace').init(); // eslint-disable-line no-unused-vars
const { randomUUID } = require('node:crypto');
const bunyan = require('bunyan');
const { pino } = require('pino');
const { pinoHttp } = require('pino-http');
// Default to INFO logs and above, unless a valid level name is provided by $LOG_LEVEL.
const logLevel = bunyan.levelFromName[(process.env.LOG_LEVEL || '').toLowerCase()] || bunyan.levelFromName.info;
// By default, log "src" data (filename, line number) if log level is "debug" or lower.
// $LOG_SRC_ENABLED overrides this behavior when set to "true"/"1" or "false"/"0".
let logSrc = logLevel <= bunyan.levelFromName.debug;
if (process.env.LOG_SRC_ENABLED === 'true' || process.env.LOG_SRC_ENABLED === '1') {
logSrc = true;
} else if (process.env.LOG_SRC_ENABLED === 'false' || process.env.LOG_SRC_ENABLED === '0') {
logSrc = false;
}
/**
* Creates a new pino logger with standard configuration defaults
* @returns pino
*/
function createLogger(options = {}) {
return pino({
name: process.env.LOG_NAME || 'gost',
level: (process.env.LOG_LEVEL || 'info').toLowerCase(),
formatters: {
level(level) {
return { level };
},
},
...options,
});
}
function createLoggerMiddleware(logger, appOptions = {}) {
return pinoHttp({
logger,
autoLogging: {
ignore: (req) => {
// Don't output auto-logs for health checks
if (req.originalUrl === '/api/health') {
return true;
}
// Disable if explicitly requested, such as during tests
if (appOptions?.disableRequestLogging) {
return true;
}
return false;
},
},
genReqId: (req) => (req.id ?? req.headers['x-amzn-trace-id'] ?? req.headers['x-request-id'] ?? randomUUID()),
serializers: {
req: (req) => {
const queryStart = req.url.lastIndexOf('?');
const path = queryStart > -1 ? req.url.substring(0, queryStart) : req.url;
return { id: req.id, method: req.method, path };
},
res: (res) => ({ statusCode: res.statusCode }),
},
});
}
module.exports = {
createLogger,
createLoggerMiddleware,
log: createLogger(),
bunyanLogger: bunyan.createLogger({
name: process.env.LOG_NAME || 'gost',
level: logLevel,
src: logSrc,
serializers: { err: bunyan.stdSerializers.err },
}),
};