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.logging.log4j; 18 19 import java.util.Locale; 20 21 /** 22 * Levels used for identifying the severity of an event. Levels are organized from most specific to least: 23 * <ul> 24 * <li>{@link #OFF} (most specific)</li> 25 * <li>{@link #FATAL}</li> 26 * <li>{@link #ERROR}</li> 27 * <li>{@link #WARN}</li> 28 * <li>{@link #INFO}</li> 29 * <li>{@link #DEBUG}</li> 30 * <li>{@link #TRACE}</li> 31 * <li>{@link #ALL} (least specific)</li> 32 * </ul> 33 * 34 * Typically, configuring a level in a filter or on a logger will cause logging events of that level and those 35 * that are more specific to pass through the filter. 36 * A special level, {@link #ALL}, is guaranteed to capture all levels when used in logging configurations. 37 */ 38 public enum Level { 39 40 /** 41 * No events will be logged. 42 */ 43 OFF(0), 44 45 /** 46 * A severe error that will prevent the application from continuing. 47 */ 48 FATAL(1), 49 50 /** 51 * An error in the application, possibly recoverable. 52 */ 53 ERROR(2), 54 55 /** 56 * An event that might possible lead to an error. 57 */ 58 WARN(3), 59 60 /** 61 * An event for informational purposes. 62 */ 63 INFO(4), 64 65 /** 66 * A general debugging event. 67 */ 68 DEBUG(5), 69 70 /** 71 * A fine-grained debug message, typically capturing the flow through the application. 72 */ 73 TRACE(6), 74 75 /** 76 * All events should be logged. 77 */ 78 ALL(Integer.MAX_VALUE); 79 80 private final int intLevel; 81 82 private Level(final int val) { 83 intLevel = val; 84 } 85 86 /** 87 * Converts the string passed as argument to a level. If the 88 * conversion fails, then this method returns {@link #DEBUG}. 89 * 90 * @param sArg The name of the desired Level. 91 * @return The Level associated with the String. 92 */ 93 public static Level toLevel(final String sArg) { 94 return toLevel(sArg, DEBUG); 95 } 96 97 /** 98 * Converts the string passed as argument to a level. If the 99 * conversion fails, then this method returns the value of 100 * <code>defaultLevel</code>. 101 * 102 * @param name The name of the desired Level. 103 * @param defaultLevel The Level to use if the String is invalid. 104 * @return The Level associated with the String. 105 */ 106 public static Level toLevel(final String name, final Level defaultLevel) { 107 if (name == null) { 108 return defaultLevel; 109 } 110 final String cleanLevel = name.toUpperCase(Locale.ENGLISH); 111 for (final Level level : values()) { 112 if (level.name().equals(cleanLevel)) { 113 return level; 114 } 115 } 116 return defaultLevel; 117 } 118 119 /** 120 * Compares this level against the level passed as an argument and returns true if this 121 * level is the same or more specific. 122 * 123 * @param level The level to check. 124 * @return True if the passed Level is more specific or the same as this Level. 125 */ 126 public boolean isAtLeastAsSpecificAs(final Level level) { 127 return intLevel <= level.intLevel; 128 } 129 130 /** 131 * Compares this level against the level passed as an argument and returns true if this 132 * level is the same or more specific. 133 * 134 * @param level The level to check. 135 * @return True if the passed Level is more specific or the same as this Level. 136 */ 137 public boolean isAtLeastAsSpecificAs(final int level) { 138 return intLevel <= level; 139 } 140 141 /** 142 * Compares the specified Level against this one. 143 * @param level The level to check. 144 * @return True if the passed Level is more specific or the same as this Level. 145 */ 146 public boolean lessOrEqual(final Level level) { 147 return intLevel <= level.intLevel; 148 } 149 150 /** 151 * Compares the specified Level against this one. 152 * @param level The level to check. 153 * @return True if the passed Level is more specific or the same as this Level. 154 */ 155 public boolean lessOrEqual(final int level) { 156 return intLevel <= level; 157 } 158 159 /** 160 * Returns the integer value of the Level. 161 * @return the integer value of the Level. 162 */ 163 public int intLevel() { 164 return intLevel; 165 } 166 }