1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic;
15
16 import org.slf4j.event.EventConstants;
17 import org.slf4j.spi.LocationAwareLogger;
18
19
20
21
22
23
24
25
26
27 public final class Level implements java.io.Serializable {
28
29 private static final long serialVersionUID = -814092767334282137L;
30
31 public static final int OFF_INT = Integer.MAX_VALUE;
32 public static final int ERROR_INT = 40000;
33 public static final int WARN_INT = 30000;
34 public static final int INFO_INT = 20000;
35 public static final int DEBUG_INT = 10000;
36 public static final int TRACE_INT = 5000;
37 public static final int ALL_INT = Integer.MIN_VALUE;
38
39 public static final Integer OFF_INTEGER = OFF_INT;
40 public static final Integer ERROR_INTEGER = ERROR_INT;
41 public static final Integer WARN_INTEGER = WARN_INT;
42 public static final Integer INFO_INTEGER = INFO_INT;
43 public static final Integer DEBUG_INTEGER = DEBUG_INT;
44 public static final Integer TRACE_INTEGER = TRACE_INT;
45 public static final Integer ALL_INTEGER = ALL_INT;
46
47
48
49
50 public static final Level OFF = new Level(OFF_INT, "OFF");
51
52
53
54
55
56 public static final Level ERROR = new Level(ERROR_INT, "ERROR");
57
58
59
60
61 public static final Level WARN = new Level(WARN_INT, "WARN");
62
63
64
65
66
67 public static final Level INFO = new Level(INFO_INT, "INFO");
68
69
70
71
72
73 public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
74
75
76
77
78
79 public static final Level TRACE = new Level(TRACE_INT, "TRACE");
80
81
82
83
84 public static final Level ALL = new Level(ALL_INT, "ALL");
85
86 public final int levelInt;
87 public final String levelStr;
88
89
90
91
92 private Level(int levelInt, String levelStr) {
93 this.levelInt = levelInt;
94 this.levelStr = levelStr;
95 }
96
97
98
99
100 public String toString() {
101 return levelStr;
102 }
103
104
105
106
107 public int toInt() {
108 return levelInt;
109 }
110
111 static public Level convertAnSLF4JLevel(org.slf4j.event.Level slf4jLevel) {
112 final int levelInt = slf4jLevel.toInt();
113 return fromLocationAwareLoggerInteger(levelInt);
114 }
115
116
117
118
119
120
121 public Integer toInteger() {
122 switch (levelInt) {
123 case ALL_INT:
124 return ALL_INTEGER;
125 case TRACE_INT:
126 return TRACE_INTEGER;
127 case DEBUG_INT:
128 return DEBUG_INTEGER;
129 case INFO_INT:
130 return INFO_INTEGER;
131 case WARN_INT:
132 return WARN_INTEGER;
133 case ERROR_INT:
134 return ERROR_INTEGER;
135 case OFF_INT:
136 return OFF_INTEGER;
137 default:
138 throw new IllegalStateException("Level " + levelStr + ", " + levelInt + " is unknown.");
139 }
140 }
141
142
143
144
145
146 public boolean isGreaterOrEqual(Level r) {
147 return levelInt >= r.levelInt;
148 }
149
150
151
152
153
154 public static Level toLevel(String sArg) {
155 return toLevel(sArg, Level.DEBUG);
156 }
157
158
159
160
161
162
163
164 public static Level valueOf(String sArg) {
165 return toLevel(sArg, Level.DEBUG);
166 }
167
168
169
170
171
172 public static Level toLevel(int val) {
173 return toLevel(val, Level.DEBUG);
174 }
175
176
177
178
179
180 public static Level toLevel(int val, Level defaultLevel) {
181 switch (val) {
182 case ALL_INT:
183 return ALL;
184 case TRACE_INT:
185 return TRACE;
186 case DEBUG_INT:
187 return DEBUG;
188 case INFO_INT:
189 return INFO;
190 case WARN_INT:
191 return WARN;
192 case ERROR_INT:
193 return ERROR;
194 case OFF_INT:
195 return OFF;
196 default:
197 return defaultLevel;
198 }
199 }
200
201
202
203
204
205 public static Level toLevel(final String sArg, Level defaultLevel) {
206 if (sArg == null) {
207 return defaultLevel;
208 }
209
210
211 final String in = sArg.trim();
212
213 if (in.equalsIgnoreCase("ALL")) {
214 return Level.ALL;
215 }
216 if (in.equalsIgnoreCase("TRACE")) {
217 return Level.TRACE;
218 }
219 if (in.equalsIgnoreCase("DEBUG")) {
220 return Level.DEBUG;
221 }
222 if (in.equalsIgnoreCase("INFO")) {
223 return Level.INFO;
224 }
225 if (in.equalsIgnoreCase("WARN")) {
226 return Level.WARN;
227 }
228 if (in.equalsIgnoreCase("ERROR")) {
229 return Level.ERROR;
230 }
231 if (in.equalsIgnoreCase("OFF")) {
232 return Level.OFF;
233 }
234 return defaultLevel;
235 }
236
237
238
239
240
241
242
243 private Object readResolve() {
244 return toLevel(this.levelInt);
245 }
246
247
248
249
250
251
252
253
254
255
256 public static Level fromLocationAwareLoggerInteger(int levelInt) {
257 Level level;
258 switch (levelInt) {
259 case EventConstants.TRACE_INT:
260 level = TRACE;
261 break;
262 case EventConstants.DEBUG_INT:
263 level = DEBUG;
264 break;
265 case EventConstants.INFO_INT:
266 level = INFO;
267 break;
268 case EventConstants.WARN_INT:
269 level = WARN;
270 break;
271 case EventConstants.ERROR_INT:
272 level = ERROR;
273 break;
274 default:
275 throw new IllegalArgumentException(levelInt + " not a valid level value");
276 }
277 return level;
278 }
279
280
281
282
283
284
285
286
287
288
289 public static int toLocationAwareLoggerInteger(Level level) {
290 if (level == null)
291 throw new IllegalArgumentException("null level parameter is not admitted");
292 switch (level.toInt()) {
293 case Level.TRACE_INT:
294 return EventConstants.TRACE_INT;
295 case Level.DEBUG_INT:
296 return EventConstants.DEBUG_INT;
297 case Level.INFO_INT:
298 return EventConstants.INFO_INT;
299 case Level.WARN_INT:
300 return EventConstants.WARN_INT;
301 case Level.ERROR_INT:
302 return EventConstants.ERROR_INT;
303 default:
304 throw new IllegalArgumentException(level + " not a valid level value");
305 }
306 }
307 }