forked from GainsNetwork-org/nft-bot-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
38 lines (32 loc) · 1.18 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
import path from "node:path"
import winston from "winston";
const customLogFormat = winston.format.printf(({ timestamp, label, level, message, ...meta }) => {
const metaFormatted = Object.getOwnPropertyNames(meta).length > 0 ? ` |> ${JSON.stringify(meta, null, 2)}` : "";
return `(${timestamp}) [${label}] ${level} ${message}${metaFormatted}`;
});
export function createLogger(label, logLevel = 'warn') {
const transports = [];
if(process.env.ENABLE_CONSOLE_LOGGING === "true") {
transports.push(new winston.transports.Console({
level: logLevel,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.label( { label } ),
winston.format.colorize(),
customLogFormat)
}));
}
if(process.env.ENABLE_FS_LOGGING === "true") {
transports.push(new winston.transports.File({
filename: path.join(process.cwd(), `./logs/${new Date(new Date(new Date().setSeconds(0)).setMilliseconds(0)).toISOString().replace(/:/g, '_')}/${label}.log`),
level: logLevel,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.label( { label } ),
customLogFormat)
}));
}
return winston.createLogger({
transports,
});
}