-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.ts
60 lines (47 loc) · 1.5 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
/* eslint-disable no-console */
import * as chalk from 'chalk';
export interface Logger {
error: (...args: any[]) => void;
log: (...args: any[]) => void;
debug: (...args: any[]) => void;
newTimer: () => Timer;
}
export class Timer {
constructor(private readonly logger: Logger, private readonly startTime = Date.now()) {}
finish(description: string): void {
const deltaInSeconds = (Date.now() - this.startTime) / 1000;
this.logger.debug(description, "took", deltaInSeconds, "seconds");
}
}
function replaceWithColor(color: (x: any) => string, args: any[]): any[] {
if (!color) color = (x: any) => x; // for jest
return args.map(x => {
return ["string", "number"].includes(typeof x) ? color(x) : x;
});
}
export class ConsoleLogger implements Logger {
error(...args: any[]): void {
console.error(...replaceWithColor(chalk.red, args));
}
log(...args: any[]): void {
console.log(...args);
}
debug(...args: any[]): void {
console.debug(...replaceWithColor(chalk.cyan, args));
}
newTimer(): Timer {
return new Timer(this);
}
}
export const consoleLogger = new ConsoleLogger();
export class NoopLogger implements Logger {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
error(...args: any[]): void {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
log(...args: any[]): void {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
debug(...args: any[]): void {}
newTimer(): Timer {
return new Timer(this);
}
}