-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ts
167 lines (140 loc) · 4.65 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
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
import { injectable } from 'inversify';
import { Config } from './../config';
import {
assignInstanceId,
assignRequestId,
assignSessionHash,
assignVersion,
removeSensitiveInfo
} from './processors';
import { consoleHandler } from './handlers';
export enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
FATAL
}
export interface LogRecord {
level: LogLevel;
message: string;
time: Date;
formatArgs: Array<string>,
extra: any;
}
export type LogProcessor = (message: LogRecord) => LogRecord;
export type ProcessorFunction = (config: Config) => LogProcessor;
export type LogHandler = (message: LogRecord) => void;
export type HandlerFunction = (config: Config) => LogHandler;
export interface LoggerOptions {
processors?: Array<ProcessorFunction>;
handlers?: Array<HandlerFunction>;
}
@injectable()
export class Logger {
private _debug = false;
private logLevel: LogLevel;
// Note that order of calls is important here, so please make sure
// that processors that are *removing* properties from log record
// are run last, otherwise you might have a nasty conflict
private defaultProcessors: Array<ProcessorFunction> = [
assignInstanceId,
assignRequestId,
assignSessionHash,
assignVersion,
removeSensitiveInfo
];
private defaultHandlers: Array<HandlerFunction> = [
consoleHandler
];
private processors: Array<LogProcessor> = [];
private handlers: Array<LogHandler> = [];
constructor(private config: Config) {
switch ((<any>this.config)['logLevel'] || LogLevel.DEBUG) {
case 'debug': this.logLevel = LogLevel.DEBUG; break;
case 'info': this.logLevel = LogLevel.INFO; break;
case 'warn': this.logLevel = LogLevel.WARN; break;
case 'error': this.logLevel = LogLevel.ERROR; break;
case 'fatal': this.logLevel = LogLevel.FATAL; break;
}
if ((<any>config)['debug'] === true || (<any>config)['debug'] === 'true') {
this._debug = true;
}
const loggerOptions: LoggerOptions = (<any>this.config)['loggerOptions']
const processors = loggerOptions && loggerOptions.processors ? loggerOptions.processors : this.defaultProcessors;
const handlers = loggerOptions && loggerOptions.handlers ? loggerOptions.handlers : this.defaultHandlers;
// Inject config into processors
processors.map((processor: ProcessorFunction) => {
this.processors.push(processor(this.config));
});
// Assign handlers
handlers.map((handler: HandlerFunction) => {
this.handlers.push(handler(this.config));
});
if (this.handlers.length === 0) {
throw TypeError('No handlers configured for logger');
}
}
public get level(): LogLevel { return this.logLevel }
public set level(logLevel: LogLevel) { this.logLevel = logLevel }
public debug(message: string, ...args: any[]) {
this.log(this.prepareRecord(LogLevel.DEBUG, message, args));
}
public info(message: string, ...args: Array<any>) {
this.log(this.prepareRecord(LogLevel.INFO, message, args));
}
public warn(message: string, ...args: any[]) {
this.log(this.prepareRecord(LogLevel.WARN, message, args));
}
public error(message: string, ...args: any[]) {
this.log(this.prepareRecord(LogLevel.ERROR, message, args));
}
public exception(error: Error, message: string, ...args: any[]) {
if (this._debug === true) {
console.error(error);
}
args.push({ error: this.formatError(error) });
this.log(this.prepareRecord(LogLevel.ERROR, message, args));
}
public fatal(message: string, ...args: any[]) {
this.log(this.prepareRecord(LogLevel.FATAL, message, args));
}
public audit(message: string, ...args: any[]) {
args.push({ audit: true });
this.log(this.prepareRecord(LogLevel.INFO, message, args));
}
private log(record: LogRecord) {
if (record.level < this.logLevel) {
return;
}
for (const processor of this.processors) {
record = processor(record);
}
for (const handler of this.handlers) {
handler(record);
}
}
private formatError(error: Error): any {
// Alternative appraoch as suggested on SO:
// return JSON.stringify(err, Object.getOwnPropertyNames(err));
return { message: error.message, stack: error.stack, name: error.name };
}
private prepareRecord(level: LogLevel, message: string, args: Array<any>): LogRecord {
const formatArgs: Array<string> = [];
const extraArgs = {};
for (const arg of args) {
if (typeof arg === 'string') {
formatArgs.push(arg);
} else {
Object.assign(extraArgs, arg);
}
}
return {
message: message,
time: new Date(),
extra: extraArgs,
formatArgs: formatArgs,
level: level
};
}
}