View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic;
11  
12  /**
13   * Defines the set of levels recognized by logback-classic, that is {@link #OFF},
14   * {@link #ERROR}, {@link #WARN}, {@link #INFO}, {@link #DEBUG},
15   * {@link #TRACE} and {@link #ALL}. <p/> The <code>Level</code> class is
16   * final and cannot be sub-classed.
17   * </p>
18   */
19  public final class Level implements java.io.Serializable {
20  
21    private static final long serialVersionUID = -814092767334282137L;
22  
23    public static final int OFF_INT = Integer.MAX_VALUE;
24    public static final int ERROR_INT = 40000;
25    public static final int WARN_INT = 30000;
26    public static final int INFO_INT = 20000;
27    public static final int DEBUG_INT = 10000;
28    public static final int TRACE_INT = 5000;
29    public static final int ALL_INT = Integer.MIN_VALUE;
30  
31    public static final Integer OFF_INTEGER = new Integer(OFF_INT);
32    public static final Integer ERROR_INTEGER = new Integer(ERROR_INT);
33    public static final Integer WARN_INTEGER = new Integer(WARN_INT);
34    public static final Integer INFO_INTEGER = new Integer(INFO_INT);
35    public static final Integer DEBUG_INTEGER = new Integer(DEBUG_INT);
36    public static final Integer TRACE_INTEGER = new Integer(DEBUG_INT);
37    public static final Integer ALL_INTEGER = new Integer(ALL_INT);
38  
39    /**
40     * The <code>OFF</code> is used to turn off logging.
41     */
42    public static final Level OFF = new Level(OFF_INT, "OFF");
43  
44    /**
45     * The <code>ERROR</code> level designates error events which may or not
46     * be fatal to the application.
47     */
48    public static final Level ERROR = new Level(ERROR_INT, "ERROR");
49  
50    /**
51     * The <code>WARN</code> level designates potentially harmful situations.
52     */
53    public static final Level WARN = new Level(WARN_INT, "WARN");
54  
55    /**
56     * The <code>INFO</code> level designates informational messages
57     * highlighting overall progress of the application.
58     */
59    public static final Level INFO = new Level(INFO_INT, "INFO");
60  
61    /**
62     * The <code>DEBUG</code> level designates informational events of lower
63     * importance.
64     */
65    public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
66  
67    /**
68     * The <code>TRACE</code> level designates informational events of very low
69     * importance.
70     */
71    public static final Level TRACE = new Level(TRACE_INT, "TRACE");
72  
73    /**
74     * The <code>ALL</code> is used to turn on all logging.
75     */
76    public static final Level ALL = new Level(ALL_INT, "ALL");
77  
78    public final int levelInt;
79    public final String levelStr;
80  
81    /**
82     * Instantiate a Level object.
83     */
84    private Level(int levelInt, String levelStr) {
85      this.levelInt = levelInt;
86      this.levelStr = levelStr;
87    }
88  
89    /**
90     * Returns the string representation of this Level.
91     */
92    public final String toString() {
93      return levelStr;
94    }
95  
96    /**
97     * Returns the integer representation of this Level.
98     */
99    public final int toInt() {
100     return levelInt;
101   }
102 
103   /**
104    * Convert a Level to an Integer object.
105    * 
106    * @return This level's Integer mapping.
107    */
108   public final Integer toInteger() {
109     switch (levelInt) {
110     case ALL_INT:
111       return ALL_INTEGER;
112     case TRACE_INT:
113       return TRACE_INTEGER;
114     case DEBUG_INT:
115       return DEBUG_INTEGER;
116     case INFO_INT:
117       return INFO_INTEGER;
118     case WARN_INT:
119       return WARN_INTEGER;
120     case ERROR_INT:
121       return ERROR_INTEGER;
122     case OFF_INT:
123       return OFF_INTEGER;
124     default:
125       throw new IllegalStateException("Level " + levelStr + ", " + levelInt
126           + " is unknown.");
127     }
128   }
129 
130   /**
131    * Returns <code>true</code> if this Level has a higher or equal Level than
132    * the Level passed as argument, <code>false</code> otherwise.
133    */
134   public boolean isGreaterOrEqual(Level r) {
135     return levelInt >= r.levelInt;
136   }
137 
138   /**
139    * Convert the string passed as argument to a Level. If the conversion fails,
140    * then this method returns {@link #DEBUG}.
141    */
142   public static Level toLevel(String sArg) {
143     return toLevel(sArg, Level.DEBUG);
144   }
145 
146 
147   /**
148    * This method exists in order to comply with Joran's valueOf convention.
149    * @param sArg
150    * @return
151    */
152   public static Level valueOf(String sArg) {
153     return toLevel(sArg, Level.DEBUG);
154   }
155 
156   
157   /**
158    * Convert an integer passed as argument to a Level. If the conversion fails,
159    * then this method returns {@link #DEBUG}.
160    */
161   public static Level toLevel(int val) {
162     return toLevel(val, Level.DEBUG);
163   }
164 
165   /**
166    * Convert an integer passed as argument to a Level. If the conversion fails,
167    * then this method returns the specified default.
168    */
169   public static Level toLevel(int val, Level defaultLevel) {
170     switch (val) {
171     case ALL_INT:
172       return ALL;
173     case TRACE_INT:
174       return TRACE;
175     case DEBUG_INT:
176       return DEBUG;
177     case INFO_INT:
178       return INFO;
179     case WARN_INT:
180       return WARN;
181     case ERROR_INT:
182       return ERROR;
183     case OFF_INT:
184       return OFF;
185     default:
186       return defaultLevel;
187     }
188   }
189 
190   /**
191    * Convert the string passed as argument to a Level. If the conversion fails,
192    * then this method returns the value of <code>defaultLevel</code>.
193    */
194   public static Level toLevel(String sArg, Level defaultLevel) {
195     if (sArg == null) {
196       return defaultLevel;
197     }
198 
199     if (sArg.equalsIgnoreCase("ALL")) {
200       return Level.ALL;
201     }
202     if (sArg.equalsIgnoreCase("TRACE")) {
203       return Level.TRACE;
204     }
205     if (sArg.equalsIgnoreCase("DEBUG")) {
206       return Level.DEBUG;
207     }
208     if (sArg.equalsIgnoreCase("INFO")) {
209       return Level.INFO;
210     }
211     if (sArg.equalsIgnoreCase("WARN")) {
212       return Level.WARN;
213     }
214     if (sArg.equalsIgnoreCase("ERROR")) {
215       return Level.ERROR;
216     }
217     if (sArg.equalsIgnoreCase("OFF")) {
218       return Level.OFF;
219     }
220     return defaultLevel;
221   }
222 
223   /**
224    * Return the flyweight instance of the level received through serizalization,
225    * i.e. 'this'.
226    * 
227    * @return The appropriate flyweight instance
228    */
229   private Object readResolve() {
230     return toLevel(this.levelInt);
231   }
232 }