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