2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 // Contributors: Kitching Simon <Simon.Kitching@orange.ch>
21 package org.apache.log4j;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.ObjectStreamException;
27 import java.io.Serializable;
30 * Defines the minimum set of levels recognized by the system, that is <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,
31 * <code>WARN</code>, <code>INFO</code>, <code>DEBUG</code> and <code>ALL</code>.
34 * The <code>Level</code> class may be subclassed to define a larger level set.
37 * @author Ceki Gülcü
39 public class Level extends Priority implements Serializable {
41 private static final String ALL_NAME = "ALL";
43 private static final String TRACE_NAME = "TRACE";
45 private static final String DEBUG_NAME = "DEBUG";
47 private static final String INFO_NAME = "INFO";
49 private static final String WARN_NAME = "WARN";
51 private static final String ERROR_NAME = "ERROR";
53 private static final String FATAL_NAME = "FATAL";
55 private static final String OFF_NAME = "OFF";
58 * TRACE level integer value.
62 public static final int TRACE_INT = 5000;
65 * The <code>OFF</code> has the highest possible rank and is intended to turn off logging.
67 final static public Level OFF = new Level(OFF_INT, OFF_NAME, 0);
70 * The <code>FATAL</code> level designates very severe error events that will presumably lead the application to abort.
72 final static public Level FATAL = new Level(FATAL_INT, FATAL_NAME, 0);
75 * The <code>ERROR</code> level designates error events that might still allow the application to continue running.
77 final static public Level ERROR = new Level(ERROR_INT, ERROR_NAME, 3);
80 * The <code>WARN</code> level designates potentially harmful situations.
82 final static public Level WARN = new Level(WARN_INT, WARN_NAME, 4);
85 * The <code>INFO</code> level designates informational messages that highlight the progress of the application at coarse-grained level.
87 final static public Level INFO = new Level(INFO_INT, INFO_NAME, 6);
90 * The <code>DEBUG</code> Level designates fine-grained informational events that are most useful to debug an application.
92 final static public Level DEBUG = new Level(DEBUG_INT, DEBUG_NAME, 7);
95 * The <code>TRACE</code> Level designates finer-grained informational events than the <code>DEBUG</code level.
99 public static final Level TRACE = new Level(TRACE_INT, TRACE_NAME, 7);
102 * The <code>ALL</code> has the lowest possible rank and is intended to turn on all logging.
104 final static public Level ALL = new Level(ALL_INT, ALL_NAME, 7);
107 * Serialization version id.
109 static final long serialVersionUID = 3491141966387921974L;
112 * Instantiate a Level object.
114 protected Level(int level, String levelStr, int syslogEquivalent) {
115 super(level, levelStr, syslogEquivalent);
119 * Convert the string passed as argument to a level. If the conversion fails, then this method returns {@link #DEBUG}.
121 public static Level toLevel(String sArg) {
122 return toLevel(sArg, Level.DEBUG);
126 * Convert an integer passed as argument to a level. If the conversion fails, then this method returns {@link #DEBUG}.
128 public static Level toLevel(int val) {
129 return toLevel(val, Level.DEBUG);
133 * Convert an integer passed as argument to a level. If the conversion fails, then this method returns the specified default.
135 public static Level toLevel(int val, Level defaultLevel) {
159 * Convert the string passed as argument to a level. If the conversion fails, then this method returns the value of
160 * <code>defaultLevel</code>.
162 public static Level toLevel(String sArg, Level defaultLevel) {
166 String s = sArg.toUpperCase();
168 if (s.equals(ALL_NAME)) {
171 if (s.equals(DEBUG_NAME)) {
174 if (s.equals(INFO_NAME)) {
177 if (s.equals(WARN_NAME)) {
180 if (s.equals(ERROR_NAME)) {
183 if (s.equals(FATAL_NAME)) {
186 if (s.equals(OFF_NAME)) {
189 if (s.equals(TRACE_NAME)) {
193 // For Turkish i problem, see bug 40937
195 if (s.equals("\u0130NFO")) {
202 * Custom deserialization of Level.
205 * serialization stream.
206 * @throws IOException
208 * @throws ClassNotFoundException
209 * if class not found.
211 private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
212 s.defaultReadObject();
214 syslogEquivalent = s.readInt();
215 levelStr = s.readUTF();
216 if (levelStr == null) {
225 * serialization stream.
226 * @throws IOException
227 * if exception during serialization.
229 private void writeObject(final ObjectOutputStream s) throws IOException {
230 s.defaultWriteObject();
232 s.writeInt(syslogEquivalent);
233 s.writeUTF(levelStr);
237 * Resolved deserialized level to one of the stock instances. May be overriden in classes derived from Level.
239 * @return resolved object.
240 * @throws ObjectStreamException
241 * if exception during resolution.
243 private Object readResolve() throws ObjectStreamException {
245 // if the deserizalized object is exactly an instance of Level
247 if (getClass() == Level.class) {
248 return toLevel(level);
251 // extension of Level can't substitute stock item