-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstatic-logger.ts
38 lines (32 loc) · 1.21 KB
/
static-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
import {LoggerConfiguration, ColoredConsoleSink, DynamicLevelSwitch, LogEventLevel, Logger} from 'serilogger';
export class StaticLogger {
private static instance: Logger;
private static levelSwitch = new DynamicLevelSwitch(LogEventLevel.information);
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static Default(): Logger {
if(!StaticLogger.instance) {
// Logger.levelSwitch.information();
StaticLogger.instance = new LoggerConfiguration()
.minLevel(StaticLogger.levelSwitch)
.writeTo(new ColoredConsoleSink({ includeTimestamps: true }))
.create()
}
return StaticLogger.instance;
}
public static CreateLoggerForSource(source: string): Logger {
return new LoggerConfiguration()
.enrich({
source: source
})
.writeTo(StaticLogger.Default())
.create();
}
public static async setLevel(input: string): Promise<boolean> {
if(!Object.keys(LogEventLevel).some(k => k === input)) return false;
const level = (LogEventLevel as any)[input]
await StaticLogger.levelSwitch.set(level)
StaticLogger.Default().info('LogLevel changed to {level}', input)
return true;
}
}