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