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
| import { Configuration, Logger, configure, shutdown } from "log4js";
export const enum ELogLevel {
ALL = 'ALL',
MARK = 'MARK',
TRACE = 'TRACE',
DEBUG = 'DEBUG',
INFO = 'INFO',
WARN = 'WARN',
ERROR = 'ERROR',
FATAL = 'FATAL',
OFF = 'OFF'
}
export class Log {
private static _instance: Log;
private _log: Logger;
public static get instance(): Log {
if (!this._instance) {
this._instance = new Log();
}
return this._instance;
}
public init(filename: string, level: ELogLevel): void {
let cfg: Configuration = {
appenders: {
console: {
type: 'console'
},
dailyFile: {
type: 'dateFile',
filename: filename,
pattern: 'yyyy-MM-dd.log',
alwaysIncludePattern: true,
maxLogSize: 10 * 1024 * 1024, // 100M
backups: 10, // Number of backup files
},
// network: { // 网络日志
// type: 'log4js-logstash',
// host: 'your-logstash-server',
// port: 5000,
// fields: {
// // 添加额外的字段,如项目名称
// appName: filename,
// },
// },
},
categories: {
default: { appenders: ['dailyFile'], level: level },
}
}
const log4 = configure(cfg);
this._log = log4.getLogger("default");
}
public debug(message: any, ...args: any[]): void {
this._log.debug(message, ...args);
}
public info(message: any, ...args: any[]): void {
this._log.info(message, ...args);
}
public warn(message: any, ...args: any[]): void {
this._log.warn(message, ...args);
}
public error(message: any, ...args: any[]): void {
this._log.error(message, ...args, new Error().stack);
}
public fatal(message: any, ...args: any[]): void {
this._log.fatal(message, ...args, new Error().stack);
}
public shutdown(cb?: (error: Error) => void): void {
shutdown(cb);
}
}
|