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 import java.util.Locale;
25
26 import org.apache.logging.log4j.util.Strings;
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>
32 * and <code>ALL</code>.
33 * <p>
34 * The <code>Level</code> class may be subclassed to define a larger
35 * level set.
36 * </p>
37 */
38 public class Level extends Priority implements Serializable {
39
40 /**
41 * TRACE level integer value.
42 *
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 */
51 public static final Level OFF = new Level(OFF_INT, "OFF", 0);
52
53 /**
54 * The <code>FATAL</code> level designates very severe error
55 * events that will presumably lead the application to abort.
56 */
57 public static final Level FATAL = new Level(FATAL_INT, "FATAL", 0);
58
59 /**
60 * The <code>ERROR</code> level designates error events that
61 * might still allow the application to continue running.
62 */
63 public static final Level ERROR = new Level(ERROR_INT, "ERROR", 3);
64
65 /**
66 * The <code>WARN</code> level designates potentially harmful situations.
67 */
68 public static final Level WARN = new Level(WARN_INT, "WARN", 4);
69
70 /**
71 * The <code>INFO</code> level designates informational messages
72 * that highlight the progress of the application at coarse-grained
73 * level.
74 */
75 public static final Level INFO = new Level(INFO_INT, "INFO", 6);
76
77 /**
78 * The <code>DEBUG</code> Level designates fine-grained
79 * informational events that are most useful to debug an
80 * application.
81 */
82 public static final 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 */
88 public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7);
89
90 /**
91 * The <code>ALL</code> has the lowest possible rank and is intended to
92 * turn on all logging.
93 */
94 public static final Level ALL = new Level(ALL_INT, "ALL", 7);
95
96 /**
97 * Serialization version id.
98 */
99 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