View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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  /**
17   * Defines the set of levels recognized by logback-classic, that is {@link #OFF},
18   * {@link #ERROR}, {@link #WARN}, {@link #INFO}, {@link #DEBUG},
19   * {@link #TRACE} and {@link #ALL}. <p/> The <code>Level</code> class is
20   * final and cannot be sub-classed.
21   * </p>
22   */
23  public final class Level implements java.io.Serializable {
24  
25    private static final long serialVersionUID = -814092767334282137L;
26  
27    public static final int OFF_INT = Integer.MAX_VALUE;
28    public static final int ERROR_INT = 40000;
29    public static final int WARN_INT = 30000;
30    public static final int INFO_INT = 20000;
31    public static final int DEBUG_INT = 10000;
32    public static final int TRACE_INT = 5000;
33    public static final int ALL_INT = Integer.MIN_VALUE;
34  
35    public static final Integer OFF_INTEGER = new Integer(OFF_INT);
36    public static final Integer ERROR_INTEGER = new Integer(ERROR_INT);
37    public static final Integer WARN_INTEGER = new Integer(WARN_INT);
38    public static final Integer INFO_INTEGER = new Integer(INFO_INT);
39    public static final Integer DEBUG_INTEGER = new Integer(DEBUG_INT);
40    public static final Integer TRACE_INTEGER = new Integer(TRACE_INT);
41    public static final Integer ALL_INTEGER = new Integer(ALL_INT);
42  
43    /**
44     * The <code>OFF</code> is used to turn off logging.
45     */
46    public static final Level OFF = new Level(OFF_INT, "OFF");
47  
48    /**
49     * The <code>ERROR</code> level designates error events which may or not
50     * be fatal to the application.
51     */
52    public static final Level ERROR = new Level(ERROR_INT, "ERROR");
53  
54    /**
55     * The <code>WARN</code> level designates potentially harmful situations.
56     */
57    public static final Level WARN = new Level(WARN_INT, "WARN");
58  
59    /**
60     * The <code>INFO</code> level designates informational messages
61     * highlighting overall progress of the application.
62     */
63    public static final Level INFO = new Level(INFO_INT, "INFO");
64  
65    /**
66     * The <code>DEBUG</code> level designates informational events of lower
67     * importance.
68     */
69    public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
70  
71    /**
72     * The <code>TRACE</code> level designates informational events of very low
73     * importance.
74     */
75    public static final Level TRACE = new Level(TRACE_INT, "TRACE");
76  
77    /**
78     * The <code>ALL</code> is used to turn on all logging.
79     */
80    public static final Level ALL = new Level(ALL_INT, "ALL");
81  
82    public final int levelInt;
83    public final String levelStr;
84  
85    /**
86     * Instantiate a Level object.
87     */
88    private Level(int levelInt, String levelStr) {
89      this.levelInt = levelInt;
90      this.levelStr = levelStr;
91    }
92  
93    /**
94     * Returns the string representation of this Level.
95     */
96    public final String toString() {
97      return levelStr;
98    }
99  
100   /**
101    * Returns the integer representation of this Level.
102    */
103   public final int toInt() {
104     return levelInt;
105   }
106 
107   /**
108    * Convert a Level to an Integer object.
109    * 
110    * @return This level's Integer mapping.
111    */
112   public final Integer toInteger() {
113     switch (levelInt) {
114     case ALL_INT:
115       return ALL_INTEGER;
116     case TRACE_INT:
117       return TRACE_INTEGER;
118     case DEBUG_INT:
119       return DEBUG_INTEGER;
120     case INFO_INT:
121       return INFO_INTEGER;
122     case WARN_INT:
123       return WARN_INTEGER;
124     case ERROR_INT:
125       return ERROR_INTEGER;
126     case OFF_INT:
127       return OFF_INTEGER;
128     default:
129       throw new IllegalStateException("Level " + levelStr + ", " + levelInt
130           + " is unknown.");
131     }
132   }
133 
134   /**
135    * Returns <code>true</code> if this Level has a higher or equal Level than
136    * the Level passed as argument, <code>false</code> otherwise.
137    */
138   public boolean isGreaterOrEqual(Level r) {
139     return levelInt >= r.levelInt;
140   }
141 
142   /**
143    * Convert the string passed as argument to a Level. If the conversion fails,
144    * then this method returns {@link #DEBUG}.
145    */
146   public static Level toLevel(String sArg) {
147     return toLevel(sArg, Level.DEBUG);
148   }
149 
150 
151   /**
152    * This method exists in order to comply with Joran's valueOf convention.
153    * @param sArg
154    * @return
155    */
156   public static Level valueOf(String sArg) {
157     return toLevel(sArg, Level.DEBUG);
158   }
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 }