-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ts
64 lines (56 loc) · 2.27 KB
/
logger.ts
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
import winston, { createLogger, transports, format, config } from 'winston';
import path from 'path';
const customLevels: config.AbstractConfigSetLevels = {
customedError: 0,
customedWarn: 1,
customedInfo: 2,
customedDebug: 3,
customedSilly: 4,
};
const customColors: config.AbstractConfigSetColors = {
customedError: 'red',
customedWarn: 'yellow',
customedInfo: 'cyan',
customedDebug: 'magenta',
customedSilly: 'gray',
};
winston.addColors(customColors);
interface CustomLevels extends winston.Logger {
customedError: (message: string, ...meta: any[]) => void;
customedWarn: (message: string, ...meta: any[]) => void;
customedInfo: (message: string, ...meta: any[]) => void;
customedDebug: (message: string, ...meta: any[]) => void;
customedSilly: (message: string, ...meta: any[]) => void;
}
interface TransformableInfo {
level: string;
message: string;
[key: string]: any;
}
const customLogger: CustomLevels = <CustomLevels>createLogger({
levels: customLevels,
format: format.combine(
format.label({ label: '[customed-server]' }),
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
format.colorize(),
format.printf((info: TransformableInfo) => `${info.timestamp} - ${info.level}: ${info.label} ${info.message}`),
),
transports: [
new transports.Console({ level: 'customedSilly' }),
new transports.File({
filename: path.join(__dirname, 'logs', 'error.log'),
level: 'customedError',
}),
new transports.File({
filename: path.join(__dirname, 'logs', 'combined.log'),
}),
],
});
customLogger.customedError = (message: string, ...meta: any[]) => customLogger.log('customedError', message, ...meta);
customLogger.customedWarn = (message: string, ...meta: any[]) => customLogger.log('customedWarn', message, ...meta);
customLogger.customedInfo = (message: string, ...meta: any[]) => customLogger.log('customedInfo', message, ...meta);
customLogger.customedDebug = (message: string, ...meta: any[]) => customLogger.log('customedDebug', message, ...meta);
customLogger.customedSilly = (message: string, ...meta: any[]) => customLogger.log('customedSilly', message, ...meta);
export { customLogger };