-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathlogger.js
62 lines (56 loc) · 1.82 KB
/
logger.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
const winston = require("winston");
/**
* @param {object} params the parameter object
* @param {string} params.agencyName the agency name to use for log tagging.
* @param {string} params.reportName the report name to use for log tagging.
* @param {string} params.scriptName the script name to use for log tagging.
* @returns {string} a standard tag for the logger to identify the specific
* agency, report, and script being processed when writing logs.
*/
const tag = ({ agencyName, reportName, scriptName }) => {
let tagString = "";
if (scriptName) {
tagString = tagString + `${scriptName}`;
}
if (reportName) {
tagString = tagString + `${tagString ? " - " : ""}${reportName}`;
}
if (agencyName) {
tagString = tagString + `${tagString ? " - " : ""}${agencyName}`;
}
return tagString;
};
const logLevel = process.env.ANALYTICS_LOG_LEVEL || "debug";
const baseLogger = winston.createLogger({
level: logLevel,
format: winston.format.printf(
(logArgs) =>
`${winston.format.colorize().colorize(logArgs.level, logArgs.level)}: ${logArgs.label ? `[${logArgs.label}]` : ``} ${logArgs.message}`,
),
transports: [
new winston.transports.Console({
level: logLevel,
}),
],
});
/**
* Creates an application logger instance.
*
* @param {object} params the parameter object
* @param {string} params.agencyName the agency name to use for log tagging.
* @param {string} params.reportName the report name to use for log tagging.
* @param {string} params.scriptName the script name to use for log tagging.
* @returns {import('winston').Logger} the configured logger instance
*/
const initialize = ({
agencyName = "",
reportName = "",
scriptName = "",
} = {}) => {
return baseLogger.child({
label: tag({ agencyName, reportName, scriptName }),
});
};
module.exports = {
initialize,
};