1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j;
18
19 import java.io.Serializable;
20 import java.util.Collection;
21 import java.util.Locale;
22 import java.util.Objects;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25
26 import org.apache.logging.log4j.spi.StandardLevel;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public final class Level implements Comparable<Level>, Serializable {
46
47 private static final long serialVersionUID = 1581082L;
48 private static final ConcurrentMap<String, Level> levels = new ConcurrentHashMap<>();
49
50
51
52
53 public static final Level OFF;
54
55
56
57
58 public static final Level FATAL;
59
60
61
62
63 public static final Level ERROR;
64
65
66
67
68 public static final Level WARN;
69
70
71
72
73 public static final Level INFO;
74
75
76
77
78 public static final Level DEBUG;
79
80
81
82
83 public static final Level TRACE;
84
85
86
87
88 public static final Level ALL;
89
90 static {
91 OFF = new Level("OFF", StandardLevel.OFF.intLevel());
92 FATAL = new Level("FATAL", StandardLevel.FATAL.intLevel());
93 ERROR = new Level("ERROR", StandardLevel.ERROR.intLevel());
94 WARN = new Level("WARN", StandardLevel.WARN.intLevel());
95 INFO = new Level("INFO", StandardLevel.INFO.intLevel());
96 DEBUG = new Level("DEBUG", StandardLevel.DEBUG.intLevel());
97 TRACE = new Level("TRACE", StandardLevel.TRACE.intLevel());
98 ALL = new Level("ALL", StandardLevel.ALL.intLevel());
99 }
100
101
102
103
104 public static final String CATEGORY = "Level";
105
106 private final String name;
107 private final int intLevel;
108 private final StandardLevel standardLevel;
109
110 private Level(final String name, final int intLevel) {
111 if (name == null || name.isEmpty()) {
112 throw new IllegalArgumentException("Illegal null Level constant");
113 }
114 if (intLevel < 0) {
115 throw new IllegalArgumentException("Illegal Level int less than zero.");
116 }
117 this.name = name;
118 this.intLevel = intLevel;
119 this.standardLevel = StandardLevel.getStandardLevel(intLevel);
120 if (levels.putIfAbsent(name, this) != null) {
121 throw new IllegalStateException("Level " + name + " has already been defined.");
122 }
123 }
124
125
126
127
128
129
130 public int intLevel() {
131 return this.intLevel;
132 }
133
134
135
136
137
138
139 public StandardLevel getStandardLevel() {
140 return standardLevel;
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public boolean isInRange(final Level minLevel, final Level maxLevel) {
154 return this.intLevel >= minLevel.intLevel && this.intLevel <= maxLevel.intLevel;
155 }
156
157
158
159
160
161
162
163
164
165 public boolean isLessSpecificThan(final Level level) {
166 return this.intLevel >= level.intLevel;
167 }
168
169
170
171
172
173
174
175
176
177 public boolean isMoreSpecificThan(final Level level) {
178 return this.intLevel <= level.intLevel;
179 }
180
181 @Override
182 @SuppressWarnings("CloneDoesntCallSuperClone")
183 public Level clone() throws CloneNotSupportedException {
184 throw new CloneNotSupportedException();
185 }
186
187 @Override
188 public int compareTo(final Level other) {
189 return intLevel < other.intLevel ? -1 : (intLevel > other.intLevel ? 1 : 0);
190 }
191
192 @Override
193 public boolean equals(final Object other) {
194 return other instanceof Level && other == this;
195 }
196
197 public Class<Level> getDeclaringClass() {
198 return Level.class;
199 }
200
201 @Override
202 public int hashCode() {
203 return this.name.hashCode();
204 }
205
206
207
208
209
210
211 public String name() {
212 return this.name;
213 }
214
215 @Override
216 public String toString() {
217 return this.name;
218 }
219
220
221
222
223
224
225
226
227 public static Level forName(final String name, final int intValue) {
228 final Level level = levels.get(name);
229 if (level != null) {
230 return level;
231 }
232 try {
233 return new Level(name, intValue);
234 } catch (final IllegalStateException ex) {
235
236 return levels.get(name);
237 }
238 }
239
240
241
242
243
244
245 public static Level getLevel(final String name) {
246 return levels.get(name);
247 }
248
249
250
251
252
253
254
255
256 public static Level toLevel(final String sArg) {
257 return toLevel(sArg, Level.DEBUG);
258 }
259
260
261
262
263
264
265
266
267
268
269 public static Level toLevel(final String name, final Level defaultLevel) {
270 if (name == null) {
271 return defaultLevel;
272 }
273 final Level level = levels.get(name.toUpperCase(Locale.ENGLISH));
274 return level == null ? defaultLevel : level;
275 }
276
277
278
279
280
281 public static Level[] values() {
282 final Collection<Level> values = Level.levels.values();
283 return values.toArray(new Level[values.size()]);
284 }
285
286
287
288
289
290
291
292
293 public static Level valueOf(final String name) {
294 Objects.requireNonNull(name, "No level name given.");
295 final String levelName = name.toUpperCase(Locale.ENGLISH);
296 final Level level = levels.get(levelName);
297 if (level != null) {
298 return level;
299 }
300 throw new IllegalArgumentException("Unknown level constant [" + levelName + "].");
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name) {
317 return Enum.valueOf(enumType, name);
318 }
319
320
321 protected Object readResolve() {
322 return Level.valueOf(this.name);
323 }
324 }
325