/
logger.ts
57 lines (46 loc) · 1.33 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
/* tslint:disable:no-console */
import chalk from 'chalk';
export interface Configuration {
silentEnabled?: boolean;
debugEnabled?: boolean;
padding?: number;
}
class Logger {
public _verbose = console.log;
public _warn = console.log;
public _debug = console.log;
public _error = console.error;
private silentEnabled: boolean;
private debugEnabled: boolean;
private padding: number;
constructor(config: Configuration = {}) {
this.silentEnabled = config.silentEnabled || false;
this.debugEnabled = config.debugEnabled || false;
this.padding = config.padding || 10;
}
public pad(str: string): string {
return str + ' '.repeat(Math.max(0, this.padding - str.length));
}
public verbose(tag: string, ...terms: string[]): void {
if (!this.silentEnabled) {
this._verbose(chalk.green(this.pad(`@${tag}`)), ...terms);
}
}
public debug(...terms: string[]): void {
if (this.debugEnabled) {
this._debug(...terms);
}
}
public warn(tag: string, ...terms: string[]): void {
if (!this.silentEnabled) {
this._warn(chalk.red(this.pad(`@${tag}`)), ...terms);
}
}
public error(error: Error | string): void {
this._error(error);
}
}
export type ILogger = Logger;
export function createLogger(config: Configuration = {}): Logger {
return new Logger(config);
}