-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
58 lines (51 loc) · 1.57 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
const { createLogger, format, transports } = require("winston");
require("winston-daily-rotate-file");
const path = require("path");
const { combine, timestamp, printf } = format;
function createTransport(options) {
return new transports.DailyRotateFile({
filename: `${path.join(
options.logDirectory || "logs",
options.source || "",
options.filename || "log"
)}-%DATE%.log`,
fileDatePattern: options.fileDatePattern || "YYYY-MM-DD",
zippedArchive: options.zippedArchive || false,
maxLogFileSize: options.maxLogFileSize || null,
maxFiles: options.maxFiles || "14d",
level: options.level,
format: format.combine(
format((info) => (info.source === options.source ? info : false))()
),
});
}
function setupLogger(options) {
const logger = createLogger({
format: combine(
timestamp({ format: options.timestampFormat || "HH:mm:ss" }),
printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: options.transports.map((transport) =>
createTransport(transport)
),
});
if (
options.addConsoleInNonProduction &&
process.env.NODE_ENV !== "production"
) {
logger.add(new transports.Console());
}
// Helper to handle custom methods like logger.error(message, source)
const customLogger = {};
["error", "warn", "info", "verbose", "debug", "silly"].forEach((level) => {
customLogger[level] = (message, source) => {
logger.log({
level,
message,
source,
});
};
});
return customLogger;
}
module.exports = setupLogger;