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