-
Notifications
You must be signed in to change notification settings - Fork 8
/
Log.ts
155 lines (137 loc) · 2.83 KB
/
Log.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
// tslint:disable:no-empty no-console variable-name
import chalk from 'chalk';
/**
* Simple logger used for both CLI and browser use.
* @category Internal
*/
export class Logger {
/** The actual "console" implementation to use. */
private impl: Console = console;
/**
* Whether debugging is enabled.
* @hidden
*/
private _debug?: boolean;
/**
* Whether quiet (error-only) is enabled.
* @hidden
*/
private _quiet?: boolean;
/**
* Whether silent (no output) is enabled.
* @hidden
*/
private _silent?: boolean;
/**
* Clear the console.
*/
public clear() {
this.impl.clear();
}
/**
* Standard console logging.
* @param parms logging parameters
*/
public log(...parms: any[]) {
if (!this._silent) {
this.impl.log(...parms);
}
}
/**
* Chatty debug logging. Enabled only if debug is enabled.
* @param parms logging parameters
*/
public trace(...parms: any[]) {
if (this._debug) {
this.impl.trace(chalk.gray(...parms));
}
}
/**
* Normal debug logging. Enabled only if debug is enabled.
* @param parms logging parameters
*/
public debug(...parms: any[]) {
if (this._debug) {
this.impl.debug(chalk.gray(...parms));
}
}
/**
* Info logging. Enabled by default.
* @param parms logging parameters
*/
public info(...parms: any[]) {
if (!this._quiet && !this._silent) {
this.impl.info(...parms);
}
}
/**
* Warning logging. Enabled by default.
* @param parms logging parameters
*/
public warn(...parms: any[]) {
if (!this._quiet && !this._silent) {
this.impl.warn(chalk.yellow(...parms));
}
}
/**
* Error logging. Enabled unless in silent mode.
* @param parms logging parameters
*/
public error(...parms: any[]) {
if (!this._silent) {
this.impl.error(chalk.red(...parms));
}
}
/**
* "Fatal" logging. Enabled unless in silent mode.
* @param parms logging parameters
*/
public fatal(...parms: any[]) {
if (!this._silent) {
this.impl.error(chalk.bold.red(...parms));
}
}
/**
* Turn on all logging.
*/
public setDebug() {
this._debug = true;
this._quiet = false;
this._silent = false;
}
/**
* Reset logging to info.
*/
public setInfo() {
this._debug = false;
this._quiet = false;
this._silent = false;
}
/**
* Turn off all logging other than error.
*/
public setQuiet() {
this._debug = false;
this._quiet = true;
this._silent = false;
}
/**
* Turn off all logging.
*/
public setSilent() {
this._debug = false;
this._quiet = true;
this._silent = true;
}
}
/**
* The default logging implementation.
* Import a logger using:
*
* ```ts
* import {log} from 'api/Logger';
* ```
*
* @category Internal
*/
export const log = new Logger();