-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathlogger.ts
More file actions
66 lines (53 loc) · 1.75 KB
/
Copy pathlogger.ts
File metadata and controls
66 lines (53 loc) · 1.75 KB
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
import { LogLevelType, SDKInitConfig, SDKLoggerApi } from './sdkRuntimeModels';
export type ILoggerConfig = Pick<SDKInitConfig, 'logLevel' | 'logger'>;
export type IConsoleLogger = Partial<Pick<SDKLoggerApi, 'error' | 'warning' | 'verbose'>>;
export class Logger {
private logLevel: LogLevelType;
private logger: IConsoleLogger;
constructor(config: ILoggerConfig) {
this.logLevel = config.logLevel ?? LogLevelType.Warning;
this.logger = config.logger ?? new ConsoleLogger();
}
public verbose(msg: string): void {
if(this.logLevel === LogLevelType.None)
return;
if (this.logger.verbose && this.logLevel === LogLevelType.Verbose) {
this.logger.verbose(msg);
}
}
public warning(msg: string): void {
if(this.logLevel === LogLevelType.None)
return;
if (this.logger.warning &&
(this.logLevel === LogLevelType.Verbose || this.logLevel === LogLevelType.Warning)) {
this.logger.warning(msg);
}
}
public error(msg: string): void {
if(this.logLevel === LogLevelType.None)
return;
if (this.logger.error) {
this.logger.error(msg);
}
}
public setLogLevel(newLogLevel: LogLevelType): void {
this.logLevel = newLogLevel;
}
}
export class ConsoleLogger implements IConsoleLogger {
public verbose(msg: string): void {
if (console && console.info) {
console.info(msg);
}
}
public error(msg: string): void {
if (console && console.error) {
console.error(msg);
}
}
public warning(msg: string): void {
if (console && console.warn) {
console.warn(msg);
}
}
}