1 /* 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 */ 17 package org.apache.log4j; 18 19 import java.io.IOException; 20 import java.io.ObjectInputStream; 21 import java.io.ObjectOutputStream; 22 import java.io.ObjectStreamException; 23 import java.io.Serializable; 24 25 /** 26 * Defines the minimum set of levels recognized by the system, that is 27 * <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>, 28 * <code>WARN</code>, <code>INFO</code, <code>DEBUG</code> 29 * and 30 * <code>ALL</code>. 31 * <p/> 32 * <p>The <code>Level</code> class may be subclassed to define a larger 33 * level set. 34 */ 35 public class Level extends Priority implements Serializable { 36 37 /** 38 * TRACE level integer value. 39 * 40 * @since 1.2.12 41 */ 42 public static final int TRACE_INT = 5000; 43 44 /** 45 * The <code>OFF</code> has the highest possible rank and is 46 * intended to turn off logging. 47 */ 48 public static final Level OFF = new Level(OFF_INT, "OFF", 0); 49 50 /** 51 * The <code>FATAL</code> level designates very severe error 52 * events that will presumably lead the application to abort. 53 */ 54 public static final Level FATAL = new Level(FATAL_INT, "FATAL", 0); 55 56 /** 57 * The <code>ERROR</code> level designates error events that 58 * might still allow the application to continue running. 59 */ 60 public static final Level ERROR = new Level(ERROR_INT, "ERROR", 3); 61 62 /** 63 * The <code>WARN</code> level designates potentially harmful situations. 64 */ 65 public static final Level WARN = new Level(WARN_INT, "WARN", 4); 66 67 /** 68 * The <code>INFO</code> level designates informational messages 69 * that highlight the progress of the application at coarse-grained 70 * level. 71 */ 72 public static final 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 */ 79 public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7); 80 81 /** 82 * The <code>TRACE</code> Level designates finer-grained 83 * informational events than the <code>DEBUG</code> level. 84 */ 85 public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7); 86 87 /** 88 * The <code>ALL</code> has the lowest possible rank and is intended to 89 * turn on all logging. 90 */ 91 public static final Level ALL = new Level(ALL_INT, "ALL", 7); 92 93 /** 94 * Serialization version id. 95 */ 96 private static final long serialVersionUID = 3491141966387921974L; 97 98 /** 99 * Instantiate a Level object. 100 * 101 * @param level The logging level. 102 * @param levelStr The level name. 103 * @param syslogEquivalent The matching syslog level. 104 */ 105 protected Level(int level, String levelStr, int syslogEquivalent) { 106 super(level, levelStr, syslogEquivalent); 107 } 108 109 110 /** 111 * Convert the string passed as argument to a level. If the 112 * conversion fails, then this method returns {@link #DEBUG}. 113 * 114 * @param sArg The level name. 115 * @return The Level. 116 */ 117 public static Level toLevel(String sArg) { 118 return toLevel(sArg, Level.DEBUG); 119 } 120 121 /** 122 * Convert an integer passed as argument to a level. If the 123 * conversion fails, then this method returns {@link #DEBUG}. 124 * 125 * @param val The integer value of the Level. 126 * @return The Level. 127 */ 128 public static Level toLevel(int val) { 129 return toLevel(val, Level.DEBUG); 130 } 131 132 /** 133 * Convert an integer passed as argument to a level. If the 134 * conversion fails, then this method returns the specified default. 135 * 136 * @param val The integer value of the Level. 137 * @param defaultLevel the default level if the integer doesn't match. 138 * @return The matching Level. 139 */ 140 public static Level toLevel(int val, Level defaultLevel) { 141 switch (val) { 142 case ALL_INT: 143 return ALL; 144 case DEBUG_INT: 145 return Level.DEBUG; 146 case INFO_INT: 147 return Level.INFO; 148 case WARN_INT: 149 return Level.WARN; 150 case ERROR_INT: 151 return Level.ERROR; 152 case FATAL_INT: 153 return Level.FATAL; 154 case OFF_INT: 155 return OFF; 156 case TRACE_INT: 157 return Level.TRACE; 158 default: 159 return defaultLevel; 160 } 161 } 162 163 /** 164 * Convert the string passed as argument to a level. If the 165 * conversion fails, then this method returns the value of 166 * <code>defaultLevel</code>. 167 * @param sArg The name of the Level. 168 * @param defaultLevel The default Level to use. 169 * @return the matching Level. 170 */ 171 public static Level toLevel(String sArg, Level defaultLevel) { 172 if (sArg == null) { 173 return defaultLevel; 174 } 175 176 String s = sArg.toUpperCase(); 177 178 if (s.equals("ALL")) { 179 return Level.ALL; 180 } 181 if (s.equals("DEBUG")) { 182 return Level.DEBUG; 183 } 184 if (s.equals("INFO")) { 185 return Level.INFO; 186 } 187 if (s.equals("WARN")) { 188 return Level.WARN; 189 } 190 if (s.equals("ERROR")) { 191 return Level.ERROR; 192 } 193 if (s.equals("FATAL")) { 194 return Level.FATAL; 195 } 196 if (s.equals("OFF")) { 197 return Level.OFF; 198 } 199 if (s.equals("TRACE")) { 200 return Level.TRACE; 201 } 202 // 203 // For Turkish i problem, see bug 40937 204 // 205 if (s.equals("\u0130NFO")) { 206 return Level.INFO; 207 } 208 return defaultLevel; 209 } 210 211 /** 212 * Custom deserialization of Level. 213 * 214 * @param s serialization stream. 215 * @throws IOException if IO exception. 216 * @throws ClassNotFoundException if class not found. 217 */ 218 private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException { 219 s.defaultReadObject(); 220 level = s.readInt(); 221 syslogEquivalent = s.readInt(); 222 levelStr = s.readUTF(); 223 if (levelStr == null) { 224 levelStr = ""; 225 } 226 } 227 228 /** 229 * Serialize level. 230 * 231 * @param s serialization stream. 232 * @throws IOException if exception during serialization. 233 */ 234 private void writeObject(final ObjectOutputStream s) throws IOException { 235 s.defaultWriteObject(); 236 s.writeInt(level); 237 s.writeInt(syslogEquivalent); 238 s.writeUTF(levelStr); 239 } 240 241 /** 242 * Resolved deserialized level to one of the stock instances. 243 * May be overriden in classes derived from Level. 244 * 245 * @return resolved object. 246 * @throws ObjectStreamException if exception during resolution. 247 */ 248 protected Object readResolve() throws ObjectStreamException { 249 // 250 // if the deserizalized object is exactly an instance of Level 251 // 252 if (getClass() == Level.class) { 253 return toLevel(level); 254 } 255 // 256 // extension of Level can't substitute stock item 257 // 258 return this; 259 } 260 261 } 262