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 */ 017 package org.apache.logging.log4j; 018 019 import java.util.Locale; 020 021 /** 022 * Levels used for identifying the severity of an event. Levels are organized from most specific to least: 023 * <ul> 024 * <li>{@link #OFF} (most specific)</li> 025 * <li>{@link #FATAL}</li> 026 * <li>{@link #ERROR}</li> 027 * <li>{@link #WARN}</li> 028 * <li>{@link #INFO}</li> 029 * <li>{@link #DEBUG}</li> 030 * <li>{@link #TRACE}</li> 031 * <li>{@link #ALL} (least specific)</li> 032 * </ul> 033 * 034 * Typically, configuring a level in a filter or on a logger will cause logging events of that level and those 035 * that are more specific to pass through the filter. 036 * A special level, {@link #ALL}, is guaranteed to capture all levels when used in logging configurations. 037 */ 038 public enum Level { 039 /** 040 * No events will be logged. 041 */ 042 OFF(0), 043 /** 044 * A severe error that will prevent the application from continuing. 045 */ 046 FATAL(1), 047 /** 048 * An error in the application, possibly recoverable. 049 */ 050 ERROR(2), 051 /** 052 * An event that might possible lead to an error. 053 */ 054 WARN(3), 055 /** 056 * An event for informational purposes. 057 */ 058 INFO(4), 059 /** 060 * A general debugging event. 061 */ 062 DEBUG(5), 063 /** 064 * A fine-grained debug message, typically capturing the flow through the application. 065 */ 066 TRACE(6), 067 /** 068 * All events should be logged. 069 */ 070 ALL(Integer.MAX_VALUE); 071 072 private final int intLevel; 073 074 private Level(final int val) { 075 intLevel = val; 076 } 077 078 /** 079 * Converts the string passed as argument to a level. If the 080 * conversion fails, then this method returns {@link #DEBUG}. 081 * 082 * @param sArg The name of the desired Level. 083 * @return The Level associated with the String. 084 */ 085 public static Level toLevel(final String sArg) { 086 return toLevel(sArg, DEBUG); 087 } 088 089 /** 090 * Converts the string passed as argument to a level. If the 091 * conversion fails, then this method returns the value of 092 * <code>defaultLevel</code>. 093 * 094 * @param name The name of the desired Level. 095 * @param defaultLevel The Level to use if the String is invalid. 096 * @return The Level associated with the String. 097 */ 098 public static Level toLevel(final String name, final Level defaultLevel) { 099 if (name == null) { 100 return defaultLevel; 101 } 102 final String cleanLevel = name.toUpperCase(Locale.ENGLISH); 103 for (final Level level : values()) { 104 if (level.name().equals(cleanLevel)) { 105 return level; 106 } 107 } 108 return defaultLevel; 109 } 110 111 /** 112 * Compares this level against the level passed as an argument and returns true if this 113 * level is the same or more specific. 114 * 115 * @param level The level to check. 116 * @return True if the passed Level is more specific or the same as this Level. 117 */ 118 public boolean isAtLeastAsSpecificAs(final Level level) { 119 return intLevel <= level.intLevel; 120 } 121 122 /** 123 * Compares this level against the level passed as an argument and returns true if this 124 * level is the same or more specific. 125 * 126 * @param level The level to check. 127 * @return True if the passed Level is more specific or the same as this Level. 128 */ 129 public boolean isAtLeastAsSpecificAs(final int level) { 130 return intLevel <= level; 131 } 132 133 /** 134 * Compares the specified Level against this one. 135 * @param level The level to check. 136 * @return True if the passed Level is more specific or the same as this Level. 137 */ 138 public boolean lessOrEqual(final Level level) { 139 return intLevel <= level.intLevel; 140 } 141 142 /** 143 * Compares the specified Level against this one. 144 * @param level The level to check. 145 * @return True if the passed Level is more specific or the same as this Level. 146 */ 147 public boolean lessOrEqual(final int level) { 148 return intLevel <= level; 149 } 150 151 /** 152 * Returns the integer value of the Level. 153 * @return the integer value of the Level. 154 */ 155 public int intLevel() { 156 return intLevel; 157 } 158 }