View Javadoc

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       * No events will be logged.
41       */
42      OFF(0),
43      /**
44       * A severe error that will prevent the application from continuing.
45       */
46      FATAL(1),
47      /**
48       * An error in the application, possibly recoverable.
49       */
50      ERROR(2),
51      /**
52       * An event that might possible lead to an error.
53       */
54      WARN(3),
55      /**
56       * An event for informational purposes.
57       */
58      INFO(4),
59      /**
60       * A general debugging event.
61       */
62      DEBUG(5),
63      /**
64       * A fine-grained debug message, typically capturing the flow through the application.
65       */
66      TRACE(6),
67      /**
68       * All events should be logged.
69       */
70      ALL(Integer.MAX_VALUE);
71  
72      private final int intLevel;
73  
74      private Level(int val) {
75          intLevel = val;
76      }
77  
78      /**
79       * Converts the string passed as argument to a level. If the
80       * conversion fails, then this method returns {@link #DEBUG}.
81       *
82       * @param sArg The name of the desired Level.
83       * @return The Level associated with the String.
84       */
85      public static Level toLevel(String sArg) {
86          return toLevel(sArg, DEBUG);
87      }
88  
89      /**
90       * Converts the string passed as argument to a level. If the
91       * conversion fails, then this method returns the value of
92       * <code>defaultLevel</code>.
93       *
94       * @param name The name of the desired Level.
95       * @param defaultLevel The Level to use if the String is invalid.
96       * @return The Level associated with the String.
97       */
98      public static Level toLevel(final String name, final Level defaultLevel) {
99          if (name == null) {
100             return defaultLevel;
101         }
102         final String cleanLevel = name.toUpperCase(Locale.ENGLISH);
103         for (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(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(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(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(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 }