This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LogService.ts
90 lines (75 loc) · 2.32 KB
/
LogService.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
// Copyright (c) 2022-2023. Heusala Group Oy. All rights reserved.
// Copyright (c) 2020-2022. Sendanor. All rights reserved.
import { LogLevel, stringifyLogLevel } from "./types/LogLevel";
import { Logger } from "./types/Logger";
import { ContextLogger } from "./logger/context/ContextLogger";
import { ConsoleLogger } from "./logger/console/ConsoleLogger";
export class LogService {
public static Level = LogLevel;
private static _level : LogLevel = LogLevel.DEBUG;
private static _logger : Logger = new ConsoleLogger();
public static setLogLevel (value : LogLevel | undefined) : Logger {
this._level = value ?? LogLevel.DEBUG;
return this;
}
public static getLogLevel () : LogLevel {
return this._level;
}
public static getLogLevelString () : string {
return stringifyLogLevel(this._level);
}
public static setLogger (value : Logger) {
if (!value) throw new TypeError(`The logger was not defined`);
this._logger = value;
}
public static getLogger () : Logger {
return this._logger;
}
/**
* Logs a debug message.
*
* @param args - The arguments to log.
* @see {@link LogLevel.DEBUG}
*/
public static debug (...args : readonly any[]) {
if (this._level <= LogLevel.DEBUG) {
this._logger.debug(...args);
}
}
/**
* Logs an info message.
*
* @param args - The arguments to log.
* @see {@link LogLevel.INFO}
*/
public static info (...args : readonly any[]) {
if (this._level <= LogLevel.INFO) {
this._logger.info(...args);
}
}
/**
* Logs a warning message.
*
* @param args - The arguments to log.
* @see @{@link LogLevel.WARN}
*/
public static warn (...args : readonly any[]) {
if (this._level <= LogLevel.WARN) {
this._logger.warn(...args);
}
}
/**
* Logs an error message.
*
* @param args - The arguments to log.
* @see {@link LogLevel.ERROR}
*/
public static error (...args : readonly any[]) {
if (this._level <= LogLevel.ERROR) {
this._logger.error(...args);
}
}
public static createLogger (name : string) : ContextLogger {
return new ContextLogger(name, LogService);
}
}