-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.js
48 lines (45 loc) · 1.32 KB
/
log.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
const winstonLoggerConf = require("winston");
require("winston-daily-rotate-file");
const {format, transports} = require("winston");
const logger = function(filename) {
return new winstonLoggerConf.createLogger({
transports: [
new transports.Console({
level: "debug",
format: format.combine(
format.colorize(),
format.errors({ stack: true }),
format.timestamp({
format: "YYYY-MM-DD HH:mm:ss",
}),
format.printf(info => `${info.timestamp} ${formatFilename(filename)} [${info.level}]: ${info.message}`)
)
}),
new transports.DailyRotateFile({
filename: "./logs/%DATE%-standard.log",
maxSize: "1g",
maxDays: "3d",
zippedArchive: true,
datePattern: "YYYY-MM-DD",
format: format.combine(
format.timestamp({
format: "YYYY-MM-DD HH:mm:ss"
}),
format.printf(info => `${info.timestamp} ${formatFilename(filename)} [${info.level}]: ${info.message}`)
)
})
],
exitOnError: false,
});
};
function formatFilename(filename) {
let maxFilenameLength = 10;
let trimmedFilename =
filename.length > maxFilenameLength
? filename.substring(0, maxFilenameLength - 2) + ".."
: filename;
trimmedFilename = `[${trimmedFilename}]`;
trimmedFilename = trimmedFilename.padEnd(maxFilenameLength + 2, " ");
return trimmedFilename;
}
module.exports = logger;