forked from mahajanankur/motifer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winstonClient.js
119 lines (108 loc) · 3.79 KB
/
winstonClient.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// const winston = require('winston');
const httpContext = require('express-http-context');
const { createLogger, format, transports, level } = require('winston');
const { combine, timestamp, label, printf, align, colorize } = format;
// const supportedLevels = ["debug", "info", "warn", "error"];
const defaultLevel = "info";
const REQUEST_ID = "requestId";
const DailyRotateFile = require('winston-daily-rotate-file');
const loggingLevels = {
crawlerror: -2,
crawlui: -2,
crawlinfo: -1,
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
};
const maskedLevels = {
crawlinfo: 'INFO',
crawlerror: 'ERROR'
};
const customFormat = printf(info => {
let requestId = httpContext.get(REQUEST_ID);
if (info.api) {
return `${info.message}`;
} else {
if (info.isExpress) {
// [${info.args ? JSON.stringify(info.args) : null}]
if(['crawlerror', 'crawlinfo'].includes(info.level)){
return `${info.timestamp} [${maskedLevels[info.level]}] ${info.message}`;
}
if(info.level === 'crawlui'){
return `${info.message}`;
}
return `${info.timestamp} [service] [${requestId ? requestId : null}] [${info.label}] [${info.level.toUpperCase()}] [${info.filename}] ${info.message}`;
} else {
if(['crawlerror', 'crawlinfo'].includes(info.level)){
return `${info.timestamp} [${maskedLevels[info.level]}] ${info.message}`;
}
if(info.level === 'crawlui'){
return `${info.message}`;
}
return `${info.timestamp} [${info.filename}] [${info.label}] [${info.level.toUpperCase()}] [${info.filename}] ${info.message}`;
}
}
});
const fileRotation = (options) => {
let transport = new DailyRotateFile({
filename: options.filename || 'motifer-%DATE%.log',
datePattern: options.datePattern || 'YYYY-MM-DD',
zippedArchive: options.archived || true,
maxSize: options.maxSize || '20m',
maxFiles: options.maxFiles || '14d',
frequency: options.frequency || null,
dirname: options.dirname || '.'
});
return transport;
}
exports.winstonLoggerClient = (level, options) => {
let logger = createLogger({
format: combine(
timestamp(),
customFormat
),
levels: loggingLevels,
transports: buildTransports(level, options)
});
return logger;
}
const buildTransports = (level, options) => {
let transporters = [];
let logLevel = verifyLogLevel(level);
// default console transports
transporters.push(new transports.Console({ level: logLevel }));
if (options && options instanceof Array) {
options.forEach(element => {
// validation check on filename.
if (!element.filename) {
throw new Error("filename is null.");
}
logLevel = verifyLogLevel(element.level);
// check if rotation is enabled
if (element.rotate) {
transporters.push(fileRotation(element));
} else {
let path = element.filename;
if (element.dirname) {
path = element.dirname + "/" + element.filename;
}
transporters.push(new transports.File({ filename: path, level: logLevel }));
}
});
} else if (options && !(options instanceof Array)) {
throw new Error("Options should be an array.");
}
return transporters;
}
const verifyLogLevel = (level) => {
let logLevel = defaultLevel;
if (level) {
level = level.toLowerCase();
logLevel = loggingLevels[level] || level == 'error' ? level : defaultLevel;
}
return logLevel;
}