-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
52 lines (43 loc) · 1.37 KB
/
Copy pathlogger.js
File metadata and controls
52 lines (43 loc) · 1.37 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
const path = require('path');
const pino = require('pino');
require('dotenv').config({ path: '.env.local' });
// If LOGGER_ENV is set to 'local', use pretty print for local development
// Otherwise, use JSON and formatiing for deployed service - e.g. Grafana Loki
const isGrafanaLogging = process.env.LOGGER_ENV !== "local";
// When running tests, NODE_ENV is automatically set to 'test' by Jest.
const isTestEnvironment = process.env.NODE_ENV === 'test';
const defaultLoggingLevel = isGrafanaLogging ? 'info' : 'debug';
const baseLogger = pino({
level: isTestEnvironment
? 'silent' // no logging in tests
: defaultLoggingLevel,
transport: !isGrafanaLogging
?
{
level: 'debug',
target: 'pino-pretty', // pretty output for local
options: {
colorize: true,
ignore: 'pid,hostname,module',
messageFormat: '[{module}] {msg}'
}
}
: undefined, // raw json for prod/Loki
formatters: {
level: (label) => {
return { level: label.toLowerCase() };
},
}
});
function getLogger(callerFilename) {
const childOptions = {
module: path.basename(callerFilename)
};
// add environment information if logging to Grafana
if (isGrafanaLogging) {
childOptions.env = process.env.NODE_ENV;
}
return baseLogger.child(childOptions);
}
module.exports = getLogger;
module.exports.baseLogger = baseLogger;