001: /*
002: * Copyright 1999-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: // Contributors: Kitching Simon <Simon.Kitching@orange.ch>
018: // Nicholas Wolff
019: package org.apache.log4j;
020:
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.ObjectStreamException;
025: import java.io.Serializable;
026:
027: /**
028: Defines the minimum set of levels recognized by the system, that is
029: <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,
030: <code>WARN</code>, <code>INFO</code, <code>DEBUG</code> and
031: <code>ALL</code>.
032:
033: <p>The <code>Level</code> class may be subclassed to define a larger
034: level set.
035:
036: @author Ceki Gülcü
037:
038: */
039: public class Level extends Priority implements Serializable {
040:
041: /**
042: * TRACE level integer value.
043: * @since 1.2.12
044: */
045: public static final int TRACE_INT = 5000;
046:
047: /**
048: The <code>OFF</code> has the highest possible rank and is
049: intended to turn off logging. */
050: final static public Level OFF = new Level(OFF_INT, "OFF", 0);
051:
052: /**
053: The <code>FATAL</code> level designates very severe error
054: events that will presumably lead the application to abort.
055: */
056: final static public Level FATAL = new Level(FATAL_INT, "FATAL", 0);
057:
058: /**
059: The <code>ERROR</code> level designates error events that
060: might still allow the application to continue running. */
061: final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3);
062:
063: /**
064: The <code>WARN</code> level designates potentially harmful situations.
065: */
066: final static public Level WARN = new Level(WARN_INT, "WARN", 4);
067:
068: /**
069: The <code>INFO</code> level designates informational messages
070: that highlight the progress of the application at coarse-grained
071: level. */
072: final static public Level INFO = new Level(INFO_INT, "INFO", 6);
073:
074: /**
075: The <code>DEBUG</code> Level designates fine-grained
076: informational events that are most useful to debug an
077: application. */
078: final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
079:
080: /**
081: * The <code>TRACE</code> Level designates finer-grained
082: * informational events than the <code>DEBUG</code level.
083: * @since 1.2.12
084: */
085: public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7);
086:
087: /**
088: The <code>ALL</code> has the lowest possible rank and is intended to
089: turn on all logging. */
090: final static public Level ALL = new Level(ALL_INT, "ALL", 7);
091:
092: /**
093: * Serialization version id.
094: */
095: static final long serialVersionUID = 3491141966387921974L;
096:
097: /**
098: Instantiate a Level object.
099: */
100: protected Level(int level, String levelStr, int syslogEquivalent) {
101: super (level, levelStr, syslogEquivalent);
102: }
103:
104: /**
105: Convert the string passed as argument to a level. If the
106: conversion fails, then this method returns {@link #DEBUG}.
107: */
108: public static Level toLevel(String sArg) {
109: return (Level) toLevel(sArg, Level.DEBUG);
110: }
111:
112: /**
113: Convert an integer passed as argument to a level. If the
114: conversion fails, then this method returns {@link #DEBUG}.
115:
116: */
117: public static Level toLevel(int val) {
118: return (Level) toLevel(val, Level.DEBUG);
119: }
120:
121: /**
122: Convert an integer passed as argument to a level. If the
123: conversion fails, then this method returns the specified default.
124: */
125: public static Level toLevel(int val, Level defaultLevel) {
126: switch (val) {
127: case ALL_INT:
128: return ALL;
129: case DEBUG_INT:
130: return Level.DEBUG;
131: case INFO_INT:
132: return Level.INFO;
133: case WARN_INT:
134: return Level.WARN;
135: case ERROR_INT:
136: return Level.ERROR;
137: case FATAL_INT:
138: return Level.FATAL;
139: case OFF_INT:
140: return OFF;
141: case TRACE_INT:
142: return Level.TRACE;
143: default:
144: return defaultLevel;
145: }
146: }
147:
148: /**
149: Convert the string passed as argument to a level. If the
150: conversion fails, then this method returns the value of
151: <code>defaultLevel</code>.
152: */
153: public static Level toLevel(String sArg, Level defaultLevel) {
154: if (sArg == null)
155: return defaultLevel;
156:
157: String s = sArg.toUpperCase();
158:
159: if (s.equals("ALL"))
160: return Level.ALL;
161: if (s.equals("DEBUG"))
162: return Level.DEBUG;
163: if (s.equals("INFO"))
164: return Level.INFO;
165: if (s.equals("WARN"))
166: return Level.WARN;
167: if (s.equals("ERROR"))
168: return Level.ERROR;
169: if (s.equals("FATAL"))
170: return Level.FATAL;
171: if (s.equals("OFF"))
172: return Level.OFF;
173: if (s.equals("TRACE"))
174: return Level.TRACE;
175: return defaultLevel;
176: }
177:
178: /**
179: * Custom deserialization of Level.
180: * @param s serialization stream.
181: * @throws IOException if IO exception.
182: * @throws ClassNotFoundException if class not found.
183: */
184: private void readObject(final ObjectInputStream s)
185: throws IOException, ClassNotFoundException {
186: s.defaultReadObject();
187: level = s.readInt();
188: syslogEquivalent = s.readInt();
189: levelStr = s.readUTF();
190: if (levelStr == null) {
191: levelStr = "";
192: }
193: }
194:
195: /**
196: * Serialize level.
197: * @param s serialization stream.
198: * @throws IOException if exception during serialization.
199: */
200: private void writeObject(final ObjectOutputStream s)
201: throws IOException {
202: s.defaultWriteObject();
203: s.writeInt(level);
204: s.writeInt(syslogEquivalent);
205: s.writeUTF(levelStr);
206: }
207:
208: /**
209: * Resolved deserialized level to one of the stock instances.
210: * May be overriden in classes derived from Level.
211: * @return resolved object.
212: * @throws ObjectStreamException if exception during resolution.
213: */
214: private Object readResolve() throws ObjectStreamException {
215: //
216: // if the deserizalized object is exactly an instance of Level
217: //
218: if (getClass() == Level.class) {
219: return toLevel(level);
220: }
221: //
222: // extension of Level can't substitute stock item
223: //
224: return this;
225: }
226: }
|