This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
/
Level.ts
106 lines (98 loc) · 2.92 KB
/
Level.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
/**
* Defines a set of standard logging levels that can be used to control logging output.
* The logging Level objects are ordered and are specified by ordered integers. Enabling logging at a given
* level also enables logging at all higher levels.
*/
export class Level {
/**
* Level inidicating all logging is disabled.
* @readonly
*/
public static readonly OFF = new Level('OFF', 0);
/**
* Level indicating only fatal messages will be output.
* @readonly
*/
public static readonly FATAL = new Level('FATAL', 1);
/**
* Level indicating only error messages or worse will be output.
* @readonly
*/
public static readonly ERROR = new Level('ERROR', 2);
/**
* Level indicating only warning messages or worse will be output.
* @readonly
*/
public static readonly WARN = new Level('WARN', 3);
/**
* Level indicating only info messages or worse will be output.
* @readonly
*/
public static readonly INFO = new Level('INFO', 4);
/**
* Level indicating only debug messages or worse will be output.
* @readonly
*/
public static readonly DEBUG = new Level('DEBUG', 5);
/**
* Level indicating trace messages or worse will be output.
* @readonly
*/
public static readonly TRACE = new Level('TRACE', 6);
/**
* Level indicating that ALL messages will be output.
* @readonly
*/
public static readonly ALL = new Level('ALL', 7);
/**
* An array containing all [[Level]]s.
* @readonly
*/
public static readonly LEVELS = [Level.FATAL, Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE];
/**
* Returns a [[Level]] object representing the `string` or `number` speicified.
*
* @param level - A value corresponding to a [[Level]].
*/
public static toLevel(level: string | number): Level {
let rval: Level;
if (typeof level === 'number' && level >= this.OFF.priority && level <= this.ALL.priority) {
rval = this.ALL_LEVELS[level];
} else if (typeof level === 'string') {
rval = this.ALL_LEVELS.find(lvl => lvl.name === level.toUpperCase());
}
return rval;
}
private static readonly ALL_LEVELS = [Level.OFF, ...Level.LEVELS, Level.ALL];
private levelName: string;
private levelPriority: number;
private constructor(name: string, priority: number) {
this.levelName = name;
this.levelPriority = priority;
}
/**
* Human readable name of this [[Level]].
*
* @readonly
*/
get name(): string {
return this.levelName;
}
/**
* The numeric priority of this [[Level]]. Higher values indicate a higher level of detail.
*
* @readonly
*/
get priority(): number {
return this.levelPriority;
}
/**
* Returns `true` if this [[Level]] is greater or equal to the priority of the supplied level.
*
* @param level - The level to be compared.
* @returns {boolean}
*/
public isGreaterOrEqual(level: Level): boolean {
return this.priority >= level.priority;
}
}