-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.mjs
184 lines (166 loc) · 5.75 KB
/
logger.mjs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import winston from 'winston';
import chalk from 'chalk';
import dotenv from 'dotenv';
// 创建自定义的颜色格式
const colorizeFormat = winston.format.printf(({ level, message, timestamp }) => {
let coloredLevel;
let colorizedMessage;
switch (level) {
case 'debug':
coloredLevel = chalk.green(level);
colorizedMessage = chalk.green(message);
break;
case 'verbose':
coloredLevel = chalk.gray(level);
colorizedMessage = chalk.gray(message);
break;
case 'http':
coloredLevel = chalk.magenta(level);
colorizedMessage = chalk.magenta(message);
break;
case 'info':
coloredLevel = chalk.blue(level);
colorizedMessage = chalk.blue(message);
break;
case 'warn':
coloredLevel = chalk.yellow(level);
colorizedMessage = chalk.yellow(message);
break;
case 'error':
coloredLevel = chalk.red(level);
colorizedMessage = chalk.red(message);
break;
default:
coloredLevel = level;
colorizedMessage = message;
}
return `${timestamp} [${coloredLevel}]: ${colorizedMessage}`;
});
const plainFormat = winston.format.printf(({ level, message, timestamp }) => {
return `${timestamp} [${level}]: ${message}`;
});
dotenv.config();
const logLevel = process.env.LOG_LEVEL || 'info';
const logger = winston.createLogger({
level: logLevel, // 日志级别,info 及以上级别的日志将被记录
format: winston.format.combine(
winston.format.timestamp(),
plainFormat
),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
colorizeFormat // 控制台使用带颜色的格式
)
}), // 输出到控制台
new winston.transports.File({
filename: 'combined.log',
format: plainFormat,
level: 'error'
}) // only capture error logs to file
],
});
function sysinfo(...args) {
if (!logger.isLevelEnabled('info')) return;
const message = args.map(arg =>
{
if (arg instanceof Error) {
// 对 Error 对象进行特殊处理,获取其 message 和 stack
return `${arg.message}\n${arg.stack}`;
} else if (typeof arg === 'object') {
// 对其他对象使用 JSON.stringify 进行格式化
return JSON.stringify(arg, null, 2);
} else {
return String(arg);
}
}
).join(' ');
logger.info(message);
}
function syswarn(...args) {
if (!logger.isLevelEnabled('warn')) return;
const message = args.map(arg =>
{
if (arg instanceof Error) {
// 对 Error 对象进行特殊处理,获取其 message 和 stack
return `${arg.message}\n${arg.stack}`;
} else if (typeof arg === 'object') {
// 对其他对象使用 JSON.stringify 进行格式化
return JSON.stringify(arg, null, 2);
} else {
return String(arg);
}
}
).join(' ');
logger.warn(message);
}
function syserror(...args) {
if (!logger.isLevelEnabled('error')) return;
const message = args.map(arg =>
{
if (arg instanceof Error) {
// 对 Error 对象进行特殊处理,获取其 message 和 stack
return `${arg.message}\n${arg.stack}`;
} else if (typeof arg === 'object') {
// 对其他对象使用 JSON.stringify 进行格式化
return JSON.stringify(arg, null, 2);
} else {
return String(arg);
}
}
).join(' ');
logger.error(message);
}
function syshttp(...args) {
if (!logger.isLevelEnabled('http')) return;
const message = args.map(arg =>
{
if (arg instanceof Error) {
// 对 Error 对象进行特殊处理,获取其 message 和 stack
return `${arg.message}\n${arg.stack}`;
} else if (typeof arg === 'object') {
// 对其他对象使用 JSON.stringify 进行格式化
return JSON.stringify(arg, null, 2);
} else {
return String(arg);
}
}
).join(' ');
logger.http(message);
}
function sysverbose(...args) {
if (!logger.isLevelEnabled('verbose')) return;
const message = args.map(arg =>
{
if (arg instanceof Error) {
// 对 Error 对象进行特殊处理,获取其 message 和 stack
return `${arg.message}\n${arg.stack}`;
} else if (typeof arg === 'object') {
// 对其他对象使用 JSON.stringify 进行格式化
return JSON.stringify(arg, null, 2);
} else {
return String(arg);
}
}
).join(' ');
logger.verbose(message);
}
function sysdebug(...args) {
if (!logger.isLevelEnabled('debug')) return;
const message = args.map(arg =>
{
if (arg instanceof Error) {
// 对 Error 对象进行特殊处理,获取其 message 和 stack
return `${arg.message}\n${arg.stack}`;
} else if (typeof arg === 'object') {
// 对其他对象使用 JSON.stringify 进行格式化
return JSON.stringify(arg, null, 2);
} else {
return String(arg);
}
}
).join(' ');
logger.debug(message);
}
export { logger, sysinfo, syswarn, syserror, syshttp, sysverbose, sysdebug };