001/* 002 * Copyright 2001-2004 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 020package org.apache.log4j; 021 022import java.io.IOException; 023import java.io.ObjectInputStream; 024import java.io.ObjectOutputStream; 025import java.io.ObjectStreamException; 026import java.io.Serializable; 027 028/** 029 * Defines the minimum set of levels recognized by the system, that is 030 * <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>, <code>WARN</code>, 031 * <code>INFO</code>, <code>DEBUG</code> and <code>ALL</code>. 032 * 033 * <p> 034 * The <code>Level</code> class may be subclassed to define a larger level set. 035 * 036 * @author Ceki Gülcü 037 * 038 */ 039public class Level extends Priority implements Serializable { 040 041 /** 042 * TRACE level integer value. 043 * 044 * @since 1.2.12 045 */ 046 public static final int TRACE_INT = 5000; 047 048 // match jboss' xlevel 049 public static final int X_TRACE_INT = DEBUG_INT - 100; 050 051 /** 052 * The <code>OFF</code> has the highest possible rank and is intended to turn 053 * off logging. 054 */ 055 final static public Level OFF = new Level(OFF_INT, "OFF", 0); 056 057 /** 058 * The <code>FATAL</code> level designates very severe error events that will 059 * presumably lead the application to abort. 060 */ 061 final static public Level FATAL = new Level(FATAL_INT, "FATAL", 0); 062 063 /** 064 * The <code>ERROR</code> level designates error events that might still allow 065 * the application to continue running. 066 */ 067 final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3); 068 069 /** 070 * The <code>WARN</code> level designates potentially harmful situations. 071 */ 072 final static public Level WARN = new Level(WARN_INT, "WARN", 4); 073 074 /** 075 * The <code>INFO</code> level designates informational messages that highlight 076 * the progress of the application at coarse-grained level. 077 */ 078 final static public Level INFO = new Level(INFO_INT, "INFO", 6); 079 080 /** 081 * The <code>DEBUG</code> Level designates fine-grained informational events 082 * that are most useful to debug an application. 083 */ 084 final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7); 085 086 /** 087 * The <code>TRACE</code> Level designates finer-grained informational events 088 * than the <code>DEBUG</code> level. 089 * 090 * @since 1.2.12 091 */ 092 public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7); 093 094 /** 095 * The <code>ALL</code> has the lowest possible rank and is intended to turn on 096 * all logging. 097 */ 098 final static public Level ALL = new Level(ALL_INT, "ALL", 7); 099 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}