View Javadoc
1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  // Contributors:  Kitching Simon <Simon.Kitching@orange.ch>
18  //                Nicholas Wolff
19  
20  package org.apache.log4j;
21  
22  import java.io.IOException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.ObjectStreamException;
26  import java.io.Serializable;
27  
28  /**
29   * Defines the minimum set of levels recognized by the system, that is
30   * <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>, <code>WARN</code>,
31   * <code>INFO</code>, <code>DEBUG</code> and <code>ALL</code>.
32   * 
33   * <p>
34   * The <code>Level</code> class may be subclassed to define a larger level set.
35   * 
36   * @author Ceki G&uuml;lc&uuml;
37   * 
38   */
39  public class Level extends Priority implements Serializable {
40  
41      /**
42       * TRACE level integer value.
43       * 
44       * @since 1.2.12
45       */
46      public static final int TRACE_INT = 5000;
47  
48      // match jboss' xlevel
49      public static final int X_TRACE_INT = DEBUG_INT - 100;
50  
51      /**
52       * The <code>OFF</code> has the highest possible rank and is intended to turn
53       * off logging.
54       */
55      final static public Level OFF = new Level(OFF_INT, "OFF", 0);
56  
57      /**
58       * The <code>FATAL</code> level designates very severe error events that will
59       * presumably lead the application to abort.
60       */
61      final static public Level FATAL = new Level(FATAL_INT, "FATAL", 0);
62  
63      /**
64       * The <code>ERROR</code> level designates error events that might still allow
65       * the application to continue running.
66       */
67      final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3);
68  
69      /**
70       * The <code>WARN</code> level designates potentially harmful situations.
71       */
72      final static public Level WARN = new Level(WARN_INT, "WARN", 4);
73  
74      /**
75       * The <code>INFO</code> level designates informational messages that highlight
76       * the progress of the application at coarse-grained level.
77       */
78      final static public Level INFO = new Level(INFO_INT, "INFO", 6);
79  
80      /**
81       * The <code>DEBUG</code> Level designates fine-grained informational events
82       * that are most useful to debug an application.
83       */
84      final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
85  
86      /**
87       * The <code>TRACE</code> Level designates finer-grained informational events
88       * than the <code>DEBUG</code> level.
89       * 
90       * @since 1.2.12
91       */
92      public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7);
93  
94      /**
95       * The <code>ALL</code> has the lowest possible rank and is intended to turn on
96       * all logging.
97       */
98      final static public Level ALL = new Level(ALL_INT, "ALL", 7);
99  
100     /**
101      * Serialization version id.
102      */
103     static final long serialVersionUID = 3491141966387921974L;
104 
105     /**
106      * Instantiate a Level object.
107      * 
108      * @param level a level
109      * @param levelStr  a level string
110      * @param syslogEquivalent  the Syslog equivalent
111      */
112     protected Level(int level, String levelStr, int syslogEquivalent) {
113         super(level, levelStr, syslogEquivalent);
114     }
115 
116     /**
117      * Convert the string passed as argument to a level. If the conversion fails,
118      * then this method returns {@link #DEBUG}.
119      * 
120      * @param sArg a string
121      * @return corresponding Level
122      * 
123      */
124     public static Level toLevel(String sArg) {
125         return (Level) toLevel(sArg, Level.DEBUG);
126     }
127 
128     /**
129      * Convert an integer passed as argument to a level. If the conversion fails,
130      * then this method returns {@link #DEBUG}.
131      * 
132      * @param val an int
133      * @return a level
134      */
135     public static Level toLevel(int val) {
136         return (Level) toLevel(val, Level.DEBUG);
137     }
138 
139     /**
140      * Convert an integer passed as argument to a level. If the conversion fails,
141      * then this method returns the specified default.
142      * 
143      * @param val a value 
144      * @param defaultLevel a defaultLevel
145      * @return corresponding Level
146      */
147     public static Level toLevel(int val, Level defaultLevel) {
148         switch (val) {
149         case ALL_INT:
150             return ALL;
151         case DEBUG_INT:
152             return Level.DEBUG;
153         case INFO_INT:
154             return Level.INFO;
155         case WARN_INT:
156             return Level.WARN;
157         case ERROR_INT:
158             return Level.ERROR;
159         case FATAL_INT:
160             return Level.FATAL;
161         case OFF_INT:
162             return OFF;
163         case TRACE_INT:
164             return Level.TRACE;
165         default:
166             return defaultLevel;
167         }
168     }
169 
170     /**
171      * Convert the string passed as argument to a level. If the conversion fails,
172      * then this method returns the value of <code>defaultLevel</code>.
173      *
174      * @param sArg a string 
175      * @param defaultLevel a default level
176      * @return corresponding level
177      */
178     public static Level toLevel(String sArg, Level defaultLevel) {
179         if (sArg == null)
180             return defaultLevel;
181 
182         String s = sArg.toUpperCase();
183 
184         if (s.equals("ALL"))
185             return Level.ALL;
186         if (s.equals("DEBUG"))
187             return Level.DEBUG;
188         if (s.equals("INFO"))
189             return Level.INFO;
190         if (s.equals("WARN"))
191             return Level.WARN;
192         if (s.equals("ERROR"))
193             return Level.ERROR;
194         if (s.equals("FATAL"))
195             return Level.FATAL;
196         if (s.equals("OFF"))
197             return Level.OFF;
198         if (s.equals("TRACE"))
199             return Level.TRACE;
200         return defaultLevel;
201     }
202 
203     /**
204      * Custom deserialization of Level.
205      * 
206      * @param s serialization stream.
207      * @throws IOException            if IO exception.
208      * @throws ClassNotFoundException if class not found.
209      */
210     private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
211         s.defaultReadObject();
212         level = s.readInt();
213         syslogEquivalent = s.readInt();
214         levelStr = s.readUTF();
215         if (levelStr == null) {
216             levelStr = "";
217         }
218     }
219 
220     /**
221      * Serialize level.
222      * 
223      * @param s serialization stream.
224      * @throws IOException if exception during serialization.
225      */
226     private void writeObject(final ObjectOutputStream s) throws IOException {
227         s.defaultWriteObject();
228         s.writeInt(level);
229         s.writeInt(syslogEquivalent);
230         s.writeUTF(levelStr);
231     }
232 
233     /**
234      * Resolved deserialized level to one of the stock instances. May be overridden
235      * in classes derived from Level.
236      * 
237      * @return resolved object.
238      * @throws ObjectStreamException if exception during resolution.
239      */
240     private Object readResolve() throws ObjectStreamException {
241         //
242         // if the deserialized object is exactly an instance of Level
243         //
244         if (getClass() == Level.class) {
245             return toLevel(level);
246         }
247         //
248         // extension of Level can't substitute stock item
249         //
250         return this;
251     }
252 }