1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.classic;
15
16 import org.slf4j.spi.LocationAwareLogger;
17
18 /**
19 * Defines the set of levels recognized by logback-classic, that is {@link #OFF},
20 * {@link #ERROR}, {@link #WARN}, {@link #INFO}, {@link #DEBUG},
21 * {@link #TRACE} and {@link #ALL}. <p/> The <code>Level</code> class is
22 * final and cannot be sub-classed.
23 * </p>
24 */
25 public final class Level implements java.io.Serializable {
26
27 private static final long serialVersionUID = -814092767334282137L;
28
29 public static final int OFF_INT = Integer.MAX_VALUE;
30 public static final int ERROR_INT = 40000;
31 public static final int WARN_INT = 30000;
32 public static final int INFO_INT = 20000;
33 public static final int DEBUG_INT = 10000;
34 public static final int TRACE_INT = 5000;
35 public static final int ALL_INT = Integer.MIN_VALUE;
36
37 public static final Integer OFF_INTEGER = OFF_INT;
38 public static final Integer ERROR_INTEGER = ERROR_INT;
39 public static final Integer WARN_INTEGER = WARN_INT;
40 public static final Integer INFO_INTEGER = INFO_INT;
41 public static final Integer DEBUG_INTEGER = DEBUG_INT;
42 public static final Integer TRACE_INTEGER = TRACE_INT;
43 public static final Integer ALL_INTEGER = ALL_INT;
44
45 /**
46 * The <code>OFF</code> is used to turn off logging.
47 */
48 public static final Level OFF = new Level(OFF_INT, "OFF");
49
50 /**
51 * The <code>ERROR</code> level designates error events which may or not
52 * be fatal to the application.
53 */
54 public static final Level ERROR = new Level(ERROR_INT, "ERROR");
55
56 /**
57 * The <code>WARN</code> level designates potentially harmful situations.
58 */
59 public static final Level WARN = new Level(WARN_INT, "WARN");
60
61 /**
62 * The <code>INFO</code> level designates informational messages
63 * highlighting overall progress of the application.
64 */
65 public static final Level INFO = new Level(INFO_INT, "INFO");
66
67 /**
68 * The <code>DEBUG</code> level designates informational events of lower
69 * importance.
70 */
71 public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
72
73 /**
74 * The <code>TRACE</code> level designates informational events of very low
75 * importance.
76 */
77 public static final Level TRACE = new Level(TRACE_INT, "TRACE");
78
79 /**
80 * The <code>ALL</code> is used to turn on all logging.
81 */
82 public static final Level ALL = new Level(ALL_INT, "ALL");
83
84 public final int levelInt;
85 public final String levelStr;
86
87 /**
88 * Instantiate a Level object.
89 */
90 private Level(int levelInt, String levelStr) {
91 this.levelInt = levelInt;
92 this.levelStr = levelStr;
93 }
94
95 /**
96 * Returns the string representation of this Level.
97 */
98 public String toString() {
99 return levelStr;
100 }
101
102 /**
103 * Returns the integer representation of this Level.
104 */
105 public int toInt() {
106 return levelInt;
107 }
108
109 /**
110 * Convert a Level to an Integer object.
111 *
112 * @return This level's Integer mapping.
113 */
114 public Integer toInteger() {
115 switch (levelInt) {
116 case ALL_INT:
117 return ALL_INTEGER;
118 case TRACE_INT:
119 return TRACE_INTEGER;
120 case DEBUG_INT:
121 return DEBUG_INTEGER;
122 case INFO_INT:
123 return INFO_INTEGER;
124 case WARN_INT:
125 return WARN_INTEGER;
126 case ERROR_INT:
127 return ERROR_INTEGER;
128 case OFF_INT:
129 return OFF_INTEGER;
130 default:
131 throw new IllegalStateException("Level " + levelStr + ", " + levelInt + " is unknown.");
132 }
133 }
134
135 /**
136 * Returns <code>true</code> if this Level has a higher or equal Level than
137 * the Level passed as argument, <code>false</code> otherwise.
138 */
139 public boolean isGreaterOrEqual(Level r) {
140 return levelInt >= r.levelInt;
141 }
142
143 /**
144 * Convert the string passed as argument to a Level. If the conversion fails,
145 * then this method returns {@link #DEBUG}.
146 */
147 public static Level toLevel(String sArg) {
148 return toLevel(sArg, Level.DEBUG);
149 }
150
151 /**
152 * This method exists in order to comply with Joran's valueOf convention.
153 *
154 * @param sArg
155 * @return
156 */
157 public static Level valueOf(String sArg) {
158 return toLevel(sArg, Level.DEBUG);
159 }
160
161 /**
162 * Convert an integer passed as argument to a Level. If the conversion fails,
163 * then this method returns {@link #DEBUG}.
164 */
165 public static Level toLevel(int val) {
166 return toLevel(val, Level.DEBUG);
167 }
168
169 /**
170 * Convert an integer passed as argument to a Level. If the conversion fails,
171 * then this method returns the specified default.
172 */
173 public static Level toLevel(int val, Level defaultLevel) {
174 switch (val) {
175 case ALL_INT:
176 return ALL;
177 case TRACE_INT:
178 return TRACE;
179 case DEBUG_INT:
180 return DEBUG;
181 case INFO_INT:
182 return INFO;
183 case WARN_INT:
184 return WARN;
185 case ERROR_INT:
186 return ERROR;
187 case OFF_INT:
188 return OFF;
189 default:
190 return defaultLevel;
191 }
192 }
193
194 /**
195 * Convert the string passed as argument to a Level. If the conversion fails,
196 * then this method returns the value of <code>defaultLevel</code>.
197 */
198 public static Level toLevel(String sArg, Level defaultLevel) {
199 if (sArg == null) {
200 return defaultLevel;
201 }
202
203 if (sArg.equalsIgnoreCase("ALL")) {
204 return Level.ALL;
205 }
206 if (sArg.equalsIgnoreCase("TRACE")) {
207 return Level.TRACE;
208 }
209 if (sArg.equalsIgnoreCase("DEBUG")) {
210 return Level.DEBUG;
211 }
212 if (sArg.equalsIgnoreCase("INFO")) {
213 return Level.INFO;
214 }
215 if (sArg.equalsIgnoreCase("WARN")) {
216 return Level.WARN;
217 }
218 if (sArg.equalsIgnoreCase("ERROR")) {
219 return Level.ERROR;
220 }
221 if (sArg.equalsIgnoreCase("OFF")) {
222 return Level.OFF;
223 }
224 return defaultLevel;
225 }
226
227 /**
228 * Return the flyweight instance of the level received through serizalization,
229 * i.e. 'this'.
230 *
231 * @return The appropriate flyweight instance
232 */
233 private Object readResolve() {
234 return toLevel(this.levelInt);
235 }
236
237 /**
238 * Convert one of the integer values defined in {@link LocationAwareLogger}
239 * interface to an instance of this class, i.e. a Level.
240 *
241 * @param levelInt An integer value representing a level as defined in LocationAwareLogger
242 * @return an instance of this class, i.e. a Level.
243 * @since 1.0.1
244 */
245 public static Level fromLocationAwareLoggerInteger(int levelInt) {
246 Level level;
247 switch (levelInt) {
248 case LocationAwareLogger.TRACE_INT:
249 level = TRACE;
250 break;
251 case LocationAwareLogger.DEBUG_INT:
252 level = DEBUG;
253 break;
254 case LocationAwareLogger.INFO_INT:
255 level = INFO;
256 break;
257 case LocationAwareLogger.WARN_INT:
258 level = WARN;
259 break;
260 case LocationAwareLogger.ERROR_INT:
261 level = ERROR;
262 break;
263 default:
264 throw new IllegalArgumentException(levelInt + " not a valid level value");
265 }
266 return level;
267 }
268
269 /**
270 * Convert this level instance to an integer value defined in the
271 * {@link LocationAwareLogger} interface.
272 *
273 * @param level The level to convert to LocationAwareLogger integer
274 * @return int An integer corresponding to this level as defined in LocationAwareLogger
275 * @since 1.0.1
276 */
277 public static int toLocationAwareLoggerInteger(Level level) {
278 if (level == null)
279 throw new IllegalArgumentException("null level parameter is not admitted");
280 switch (level.toInt()) {
281 case Level.TRACE_INT:
282 return LocationAwareLogger.TRACE_INT;
283 case Level.DEBUG_INT:
284 return LocationAwareLogger.DEBUG_INT;
285 case Level.INFO_INT:
286 return LocationAwareLogger.INFO_INT;
287 case Level.WARN_INT:
288 return LocationAwareLogger.WARN_INT;
289 case Level.ERROR_INT:
290 return LocationAwareLogger.ERROR_INT;
291 default:
292 throw new IllegalArgumentException(level + " not a valid level value");
293 }
294 }
295 }
296