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