/
node-logger.ts
82 lines (69 loc) · 2.23 KB
/
node-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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as winston from "winston";
import * as DailyRotateFile from "winston-daily-rotate-file";
import * as Transport from "winston-transport";
// import { Constants } from "../client-constants";
import { SanitizedError } from "../error";
import { Logger } from "./base-logger";
import { PrettyStream } from "./pretty-stream";
const stream = new PrettyStream();
stream.pipe(process.stderr);
export interface NodeLoggerConfig {
name: string;
path?: string;
json?: boolean;
}
/**
* Logger helper class that will log
*/
export class NodeLogger implements Logger {
private _logger: winston.Logger;
constructor(config: NodeLoggerConfig) {
if (!config) {
throw new SanitizedError("Missing configuration for Logger");
}
const transports: Transport[] = [
new winston.transports.Console({
format: winston.format.combine(
winston.format.label({ label: config.name, message: true }),
winston.format.colorize(),
winston.format.simple(),
),
}),
];
if (config.path) {
let format;
if (config.json) {
format = winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
);
} else {
format = winston.format.simple();
}
transports.push(new DailyRotateFile({
maxFiles: 3,
filename: config.path,
format,
}));
}
this._logger = winston.createLogger({
level: "debug",
transports,
});
}
public log(level: string, message: string, ...params: any[]) {
this._logger.log(level, message, params);
}
public debug(message: string, ...params: any[]) {
this._logger.debug(message, ...params);
}
public info(message: string, ...params: any[]) {
this._logger.info(message, ...params);
}
public warn(message: string, ...params: any[]) {
this._logger.warn(message, ...params);
}
public error(message: string, error?: any) {
this._logger.error(message, error);
}
}