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  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.io.ObjectStreamException;
25  import java.io.Serializable;
26  
27  /**
28     Defines the minimum set of levels recognized by the system, that is
29     <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,
30     <code>WARN</code>, <code>INFO</code>, <code>DEBUG</code> and
31     <code>ALL</code>.
32  
33     <p>The <code>Level</code> class may be subclassed to define a larger
34     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      * @since 1.2.12
44      */
45    public static final int TRACE_INT = 5000;
46  
47    // match jboss' xlevel
48    public static final int X_TRACE_INT = DEBUG_INT - 100;
49  
50    /**
51       The <code>OFF</code> has the highest possible rank and is
52       intended to turn off logging.  */
53    final static public Level OFF = new Level(OFF_INT, "OFF", 0);
54  
55    /**
56       The <code>FATAL</code> level designates very severe error
57       events that will presumably lead the application to abort.
58     */
59    final static public Level FATAL = new Level(FATAL_INT, "FATAL", 0);
60  
61    /**
62       The <code>ERROR</code> level designates error events that
63       might still allow the application to continue running.  */
64    final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3);
65  
66    /**
67       The <code>WARN</code> level designates potentially harmful situations.
68    */
69    final static public Level WARN  = new Level(WARN_INT, "WARN",  4);
70  
71    /**
72       The <code>INFO</code> level designates informational messages
73       that highlight the progress of the application at coarse-grained
74       level.  */
75    final static public Level INFO  = new Level(INFO_INT, "INFO",  6);
76  
77    /**
78       The <code>DEBUG</code> Level designates fine-grained
79       informational events that are most useful to debug an
80       application.  */
81    final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
82  
83    /**
84      * The <code>TRACE</code> Level designates finer-grained
85      * informational events than the <code>DEBUG</code level.
86     *  @since 1.2.12
87      */
88    public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7);
89  
90  
91    /**
92       The <code>ALL</code> has the lowest possible rank and is intended to
93       turn on all logging.  */
94    final static public Level ALL = new Level(ALL_INT, "ALL", 7);
95  
96    /**
97     * Serialization version id.
98     */
99    static final long serialVersionUID = 3491141966387921974L;
100 
101   /**
102      Instantiate a Level object.
103    */
104   protected
105   Level(int level, String levelStr, int syslogEquivalent) {
106     super(level, levelStr, syslogEquivalent);
107   }
108 
109 
110   /**
111      Convert the string passed as argument to a level. If the
112      conversion fails, then this method returns {@link #DEBUG}. 
113   */
114   public
115   static
116   Level toLevel(String sArg) {
117     return (Level) toLevel(sArg, Level.DEBUG);
118   }
119 
120   /**
121     Convert an integer passed as argument to a level. If the
122     conversion fails, then this method returns {@link #DEBUG}.
123 
124   */
125   public
126   static
127   Level toLevel(int val) {
128     return (Level) toLevel(val, Level.DEBUG);
129   }
130 
131   /**
132     Convert an integer passed as argument to a level. If the
133     conversion fails, then this method returns the specified default.
134   */
135   public
136   static
137   Level toLevel(int val, Level defaultLevel) {
138     switch(val) {
139     case ALL_INT: return ALL;
140     case DEBUG_INT: return Level.DEBUG;
141     case INFO_INT: return Level.INFO;
142     case WARN_INT: return Level.WARN;
143     case ERROR_INT: return Level.ERROR;
144     case FATAL_INT: return Level.FATAL;
145     case OFF_INT: return OFF;
146     case TRACE_INT: return Level.TRACE;
147     default: return defaultLevel;
148     }
149   }
150 
151   /**
152      Convert the string passed as argument to a level. If the
153      conversion fails, then this method returns the value of
154      <code>defaultLevel</code>.  
155   */
156   public
157   static
158   Level toLevel(String sArg, Level defaultLevel) {                  
159     if(sArg == null)
160        return defaultLevel;
161     
162     String s = sArg.toUpperCase();
163 
164     if(s.equals("ALL")) return Level.ALL; 
165     if(s.equals("DEBUG")) return Level.DEBUG; 
166     if(s.equals("INFO"))  return Level.INFO;
167     if(s.equals("WARN"))  return Level.WARN;  
168     if(s.equals("ERROR")) return Level.ERROR;
169     if(s.equals("FATAL")) return Level.FATAL;
170     if(s.equals("OFF")) return Level.OFF;
171     if(s.equals("TRACE")) return Level.TRACE;
172     return defaultLevel;
173   }
174 
175     /**
176      * Custom deserialization of Level.
177      * @param s serialization stream.
178      * @throws IOException if IO exception.
179      * @throws ClassNotFoundException if class not found.
180      */
181     private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
182       s.defaultReadObject();
183       level = s.readInt();
184       syslogEquivalent = s.readInt();
185       levelStr = s.readUTF();
186       if (levelStr == null) {
187           levelStr = "";
188       }
189     }
190 
191     /**
192      * Serialize level.
193      * @param s serialization stream.
194      * @throws IOException if exception during serialization.
195      */
196     private void writeObject(final ObjectOutputStream s) throws IOException {
197         s.defaultWriteObject();
198         s.writeInt(level);
199         s.writeInt(syslogEquivalent);
200         s.writeUTF(levelStr);
201     }
202 
203     /**
204      * Resolved deserialized level to one of the stock instances.
205      * May be overriden in classes derived from Level.
206      * @return resolved object.
207      * @throws ObjectStreamException if exception during resolution.
208      */
209     private Object readResolve() throws ObjectStreamException {
210         //
211         //  if the deserizalized object is exactly an instance of Level
212         //
213         if (getClass() == Level.class) {
214             return toLevel(level);
215         }
216         //
217         //   extension of Level can't substitute stock item
218         //
219         return this;
220     }
221 }