-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.ts
162 lines (142 loc) · 3.39 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
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
import { LogLevel } from './types';
export default class Logger {
logLevel: LogLevel = 'info';
private scope: string;
private enabled: boolean = true;
constructor({
scope,
logLevel,
enabled = true,
}: {
scope: string;
logLevel?: LogLevel;
enabled?: boolean;
}) {
this.scope = scope;
if (logLevel) {
this.logLevel = logLevel;
}
if (typeof enabled === 'boolean') {
this.enabled = enabled;
}
}
disable() {
this.enabled = false;
}
enable() {
this.enabled = true;
}
private formatMessage(logLevel: LogLevel, message: string): string {
let tag = 'Info';
if (logLevel == 'error') {
tag = 'Error';
} else if (logLevel == 'warn') {
tag = 'Warning';
} else if (logLevel == 'debug') {
tag = 'Debug';
}
return `%c${tag}%c %c${this.scope}%c ${message}`;
}
private getTagStyle(logLevel: LogLevel): string {
let tagStyle = `
padding: 2px 4px;
border-radius: 4px;
font-weight: bold';
`;
if (logLevel == 'error') {
tagStyle = `
color: white;
background-color: #FF0012;
${tagStyle}
`;
} else if (logLevel == 'warn') {
tagStyle = `
color: black;
background-color: #FFD900;
${tagStyle}
`;
} else if (logLevel == 'info') {
tagStyle = `
color: white;
background-color: #0084B0;
${tagStyle}
`;
} else if (logLevel == 'debug') {
tagStyle = `
color: white;
background-color: #5BE300;
${tagStyle}
`;
}
return tagStyle;
}
private log(logLevel: LogLevel, message: string) {
const scopeStyle = `
color: white;
background-color: #454545;
padding: 2px 4px;
border-radius: 4px;
font-weight: bold';
`;
if (this.enabled) {
if (logLevel === 'debug' || logLevel === 'info') {
console.log(
this.formatMessage(logLevel, message),
this.getTagStyle(logLevel),
'',
scopeStyle,
'color: black'
);
} else if (logLevel === 'warn') {
console.warn(
this.formatMessage(logLevel, message),
this.getTagStyle(logLevel),
'',
scopeStyle,
'color: black'
);
} else if (logLevel === 'error') {
console.error(
this.formatMessage(logLevel, message),
this.getTagStyle(logLevel),
'',
scopeStyle,
'color: black'
);
}
}
}
private argsToString(args: any[]): string {
let message = '';
for (const arg of args) {
if (
typeof arg === 'string' ||
typeof arg === 'number' ||
typeof arg === 'boolean'
) {
message += arg + ',\n';
} else {
message += JSON.stringify(arg, undefined, 4) + ',\n';
}
}
return message.replace(/,\n$/, '');
}
debug(...args: any[]) {
if (this.logLevel === 'debug') {
this.log('debug', this.argsToString(args));
}
}
info(...args: any[]) {
if (['debug', 'info'].includes(this.logLevel)) {
this.log('info', this.argsToString(args));
}
}
warn(...args: any[]) {
if (['debug', 'info', 'warn'].includes(this.logLevel)) {
this.log('warn', this.argsToString(args));
}
}
error(...args: any[]) {
this.log('error', this.argsToString(args));
}
}