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        /**
041         * No events will be logged.
042         */
043        OFF(0),
044    
045        /**
046         * A severe error that will prevent the application from continuing.
047         */
048        FATAL(1),
049    
050        /**
051         * An error in the application, possibly recoverable.
052         */
053        ERROR(2),
054    
055        /**
056         * An event that might possible lead to an error.
057         */
058        WARN(3),
059    
060        /**
061         * An event for informational purposes.
062         */
063        INFO(4),
064    
065        /**
066         * A general debugging event.
067         */
068        DEBUG(5),
069    
070        /**
071         * A fine-grained debug message, typically capturing the flow through the application.
072         */
073        TRACE(6),
074    
075        /**
076         * All events should be logged.
077         */
078        ALL(Integer.MAX_VALUE);
079    
080        private final int intLevel;
081    
082        private Level(final int val) {
083            intLevel = val;
084        }
085    
086        /**
087         * Converts the string passed as argument to a level. If the
088         * conversion fails, then this method returns {@link #DEBUG}.
089         *
090         * @param sArg The name of the desired Level.
091         * @return The Level associated with the String.
092         */
093        public static Level toLevel(final String sArg) {
094            return toLevel(sArg, DEBUG);
095        }
096    
097        /**
098         * Converts the string passed as argument to a level. If the
099         * 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    }