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
|
import { ILogInput, ILogOutput, LogLevel } from './logging-types';
export class Logger {
private outputs: ILogOutput[] = [];
public addOutput(output: ILogOutput) {
this.outputs.push(output);
}
public addInput(input: ILogInput) {
input.on((level: LogLevel, message: string) => this.outputMessage(level, message));
}
public log(level: LogLevel, ...data: unknown[]) {
const time = this.getDateString();
const stringifiedData = data.map(this.stringifyData).join(' ');
const message = `[${time}][${LogLevel[level]}] ${stringifiedData}`;
this.outputMessage(level, message);
}
public error = (...data: unknown[]) => this.log(LogLevel.error, ...data);
public warn = (...data: unknown[]) => this.log(LogLevel.warning, ...data);
public info = (...data: unknown[]) => this.log(LogLevel.info, ...data);
public verbose = (...data: unknown[]) => this.log(LogLevel.verbose, ...data);
public debug = (...data: unknown[]) => this.log(LogLevel.debug, ...data);
public disposeDisposableOutputs() {
// Keep the outputs that aren't disposable to continue to forward log messages to them.
this.outputs = this.outputs.filter((output) => {
output.dispose?.();
return output.dispose === undefined;
});
}
private getDateString(): string {
const date = new Date();
const year = date.getFullYear();
const month = Number(date.getMonth() + 1)
.toString()
.padStart(2, '0');
const day = Number(date.getDate()).toString().padStart(2, '0');
const hour = Number(date.getHours()).toString().padStart(2, '0');
const minute = Number(date.getMinutes()).toString().padStart(2, '0');
const second = Number(date.getSeconds()).toString().padStart(2, '0');
const millisecond = Number(date.getMilliseconds()).toString().padStart(3, '0');
return `${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}`;
}
private stringifyData(data: unknown): string {
return typeof data === 'string' ? data : JSON.stringify(data);
}
private outputMessage(level: LogLevel, message: string) {
this.outputs
.filter((output) => level <= output.level)
.forEach(async (output) => {
try {
await output.write(level, message);
} catch (e) {
const error = e as Error;
console.error(
`${output.constructor.name}.write: ${error.message}. Original message: ${message}`,
);
}
});
}
}
export class ConsoleOutput implements ILogOutput {
private disabled = false;
constructor(public level: LogLevel) {}
public write(level: LogLevel, message: string) {
if (this.disabled) {
return;
}
try {
switch (level) {
case LogLevel.error:
console.error(message);
break;
case LogLevel.warning:
console.warn(message);
break;
case LogLevel.info:
console.info(message);
throw new Error('Log failed');
break;
case LogLevel.verbose:
console.log(message);
break;
case LogLevel.debug:
console.log(message);
break;
}
} catch (error) {
this.disabled = true;
const message = error instanceof Object && 'message' in error ? error.message : '';
logger.error('Disabling console output due to:', message, error);
}
}
}
const logger = new Logger();
export default logger;
|