source: trip-planner-front/node_modules/log4js/lib/levels.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.1 KB
Line 
1
2
3const configuration = require('./configuration');
4
5const validColours = [
6 'white', 'grey', 'black',
7 'blue', 'cyan', 'green',
8 'magenta', 'red', 'yellow'
9];
10
11class Level {
12 constructor(level, levelStr, colour) {
13 this.level = level;
14 this.levelStr = levelStr;
15 this.colour = colour;
16 }
17
18 toString() {
19 return this.levelStr;
20 }
21
22 /**
23 * converts given String to corresponding Level
24 * @param {(Level|string)} sArg -- String value of Level OR Log4js.Level
25 * @param {Level} [defaultLevel] -- default Level, if no String representation
26 * @return {Level}
27 */
28 static getLevel(sArg, defaultLevel) {
29 if (!sArg) {
30 return defaultLevel;
31 }
32
33 if (sArg instanceof Level) {
34 return sArg;
35 }
36
37 // a json-serialised level won't be an instance of Level (see issue #768)
38 if (sArg instanceof Object && sArg.levelStr) {
39 sArg = sArg.levelStr;
40 }
41
42 return Level[sArg.toString().toUpperCase()] || defaultLevel;
43 }
44
45 static addLevels(customLevels) {
46 if (customLevels) {
47 const levels = Object.keys(customLevels);
48 levels.forEach((l) => {
49 const levelStr = l.toUpperCase();
50 Level[levelStr] = new Level(
51 customLevels[l].value,
52 levelStr,
53 customLevels[l].colour
54 );
55 const existingLevelIndex = Level.levels.findIndex(lvl => lvl.levelStr === levelStr);
56 if (existingLevelIndex > -1) {
57 Level.levels[existingLevelIndex] = Level[levelStr];
58 } else {
59 Level.levels.push(Level[levelStr]);
60 }
61 });
62 Level.levels.sort((a, b) => a.level - b.level);
63 }
64 }
65
66
67 isLessThanOrEqualTo(otherLevel) {
68 if (typeof otherLevel === 'string') {
69 otherLevel = Level.getLevel(otherLevel);
70 }
71 return this.level <= otherLevel.level;
72 }
73
74 isGreaterThanOrEqualTo(otherLevel) {
75 if (typeof otherLevel === 'string') {
76 otherLevel = Level.getLevel(otherLevel);
77 }
78 return this.level >= otherLevel.level;
79 }
80
81 isEqualTo(otherLevel) {
82 if (typeof otherLevel === 'string') {
83 otherLevel = Level.getLevel(otherLevel);
84 }
85 return this.level === otherLevel.level;
86 }
87}
88
89Level.levels = [];
90Level.addLevels({
91 ALL: { value: Number.MIN_VALUE, colour: 'grey' },
92 TRACE: { value: 5000, colour: 'blue' },
93 DEBUG: { value: 10000, colour: 'cyan' },
94 INFO: { value: 20000, colour: 'green' },
95 WARN: { value: 30000, colour: 'yellow' },
96 ERROR: { value: 40000, colour: 'red' },
97 FATAL: { value: 50000, colour: 'magenta' },
98 MARK: { value: 9007199254740992, colour: 'grey' }, // 2^53
99 OFF: { value: Number.MAX_VALUE, colour: 'grey' }
100});
101
102configuration.addListener((config) => {
103 const levelConfig = config.levels;
104 if (levelConfig) {
105 configuration.throwExceptionIf(
106 config,
107 configuration.not(configuration.anObject(levelConfig)),
108 'levels must be an object'
109 );
110 const newLevels = Object.keys(levelConfig);
111 newLevels.forEach((l) => {
112 configuration.throwExceptionIf(
113 config,
114 configuration.not(configuration.validIdentifier(l)),
115 `level name "${l}" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)`
116 );
117 configuration.throwExceptionIf(
118 config,
119 configuration.not(configuration.anObject(levelConfig[l])),
120 `level "${l}" must be an object`
121 );
122 configuration.throwExceptionIf(
123 config,
124 configuration.not(levelConfig[l].value),
125 `level "${l}" must have a 'value' property`
126 );
127 configuration.throwExceptionIf(
128 config,
129 configuration.not(configuration.anInteger(levelConfig[l].value)),
130 `level "${l}".value must have an integer value`
131 );
132 configuration.throwExceptionIf(
133 config,
134 configuration.not(levelConfig[l].colour),
135 `level "${l}" must have a 'colour' property`
136 );
137 configuration.throwExceptionIf(
138 config,
139 configuration.not(validColours.indexOf(levelConfig[l].colour) > -1),
140 `level "${l}".colour must be one of ${validColours.join(', ')}`
141 );
142 });
143 }
144});
145
146configuration.addListener((config) => {
147 Level.addLevels(config.levels);
148});
149
150module.exports = Level;
Note: See TracBrowser for help on using the repository browser.