-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
logger.service.ts
176 lines (150 loc) · 4.71 KB
/
logger.service.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
168
169
170
171
172
173
174
175
176
import * as clc from 'cli-color';
import { Injectable } from '../decorators/core/injectable.decorator';
import { Optional } from '../decorators/core/optional.decorator';
import { isObject } from '../utils/shared.utils';
declare const process: any;
const yellow = clc.xterm(3);
export type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose';
export interface LoggerService {
log(message: any, context?: string);
error(message: any, trace?: string, context?: string);
warn(message: any, context?: string);
debug?(message: any, context?: string);
verbose?(message: any, context?: string);
}
@Injectable()
export class Logger implements LoggerService {
private static logLevels: LogLevel[] = [
'log',
'error',
'warn',
'debug',
'verbose',
];
private static lastTimestamp?: number;
private static instance?: typeof Logger | LoggerService = Logger;
constructor(
@Optional() protected context?: string,
@Optional() private readonly isTimestampEnabled = false,
) {}
error(message: any, trace = '', context?: string) {
const instance = this.getInstance();
if (!this.isLogLevelEnabled('error')) {
return;
}
instance &&
instance.error.call(instance, message, trace, context || this.context);
}
log(message: any, context?: string) {
this.callFunction('log', message, context);
}
warn(message: any, context?: string) {
this.callFunction('warn', message, context);
}
debug(message: any, context?: string) {
this.callFunction('debug', message, context);
}
verbose(message: any, context?: string) {
this.callFunction('verbose', message, context);
}
setContext(context: string) {
this.context = context;
}
static overrideLogger(logger: LoggerService | LogLevel[] | boolean) {
if (Array.isArray(logger)) {
this.logLevels = logger;
return;
}
this.instance = isObject(logger) ? (logger as LoggerService) : undefined;
}
static log(message: any, context = '', isTimeDiffEnabled = true) {
this.printMessage(message, clc.green, context, isTimeDiffEnabled);
}
static error(
message: any,
trace = '',
context = '',
isTimeDiffEnabled = true,
) {
this.printMessage(message, clc.red, context, isTimeDiffEnabled);
this.printStackTrace(trace);
}
static warn(message: any, context = '', isTimeDiffEnabled = true) {
this.printMessage(message, clc.yellow, context, isTimeDiffEnabled);
}
static debug(message: any, context = '', isTimeDiffEnabled = true) {
this.printMessage(message, clc.magentaBright, context, isTimeDiffEnabled);
}
static verbose(message: any, context = '', isTimeDiffEnabled = true) {
this.printMessage(message, clc.cyanBright, context, isTimeDiffEnabled);
}
private callFunction(
name: 'log' | 'warn' | 'debug' | 'verbose',
message: any,
context?: string,
) {
if (!this.isLogLevelEnabled(name)) {
return;
}
const instance = this.getInstance();
const func = instance && (instance as typeof Logger)[name];
func &&
func.call(
instance,
message,
context || this.context,
this.isTimestampEnabled,
);
}
private getInstance(): typeof Logger | LoggerService {
const { instance } = Logger;
return instance === this ? Logger : instance;
}
private isLogLevelEnabled(level: LogLevel): boolean {
return Logger.logLevels.includes(level);
}
private static printMessage(
message: any,
color: (message: string) => string,
context = '',
isTimeDiffEnabled?: boolean,
) {
const output = isObject(message)
? `${color('Object:')}\n${JSON.stringify(message, null, 2)}\n`
: color(message);
const localeStringOptions = {
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
day: '2-digit',
month: '2-digit',
};
const timestamp = new Date(Date.now()).toLocaleString(
undefined,
localeStringOptions,
);
const pidMessage = color(`[Nest] ${process.pid} - `);
const contextMessage = context ? yellow(`[${context}] `) : '';
const timestampDiff = this.updateAndGetTimestampDiff(isTimeDiffEnabled);
process.stdout.write(
`${pidMessage}${timestamp} ${contextMessage}${output}${timestampDiff}\n`,
);
}
private static updateAndGetTimestampDiff(
isTimeDiffEnabled?: boolean,
): string {
const includeTimestamp = Logger.lastTimestamp && isTimeDiffEnabled;
const result = includeTimestamp
? yellow(` +${Date.now() - Logger.lastTimestamp}ms`)
: '';
Logger.lastTimestamp = Date.now();
return result;
}
private static printStackTrace(trace: string) {
if (!trace) {
return;
}
process.stdout.write(`${trace}\n`);
}
}