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