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
    18  // Contributors:  Kitching Simon <Simon.Kitching@orange.ch>
    19  //                Nicholas Wolff
    20
    21  package org.apache.log4j;
    22  import java.io.IOException;
    23  import java.io.ObjectInputStream;
    24  import java.io.ObjectOutputStream;
    25  import java.io.ObjectStreamException;
    26  import java.io.Serializable;
    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> and
    32     <code>ALL</code>.
    33
    34     <p>The <code>Level</code> class may be subclassed to define a larger
    35     level set.
    36
    37     @author Ceki G&uuml;lc&uuml;
    38
    39   */
    40  public class Level extends Priority implements Serializable {
    41
    42     /***
    43      * TRACE level integer value.
    44      * @since 1.2.12
    45      */
    46    public static final int TRACE_INT = 5000;
    47
    48    /***
    49       The <code>OFF</code> has the highest possible rank and is
    50       intended to turn off logging.  */
    51    final static public 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    final static public 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    final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3);
    63
    64    /***
    65       The <code>WARN</code> level designates potentially harmful situations.
    66    */
    67    final static public Level WARN  = new Level(WARN_INT, "WARN",  4);
    68
    69    /***
    70       The <code>INFO</code> level designates informational messages
    71       that highlight the progress of the application at coarse-grained
    72       level.  */
    73    final static public Level INFO  = new Level(INFO_INT, "INFO",  6);
    74
    75    /***
    76       The <code>DEBUG</code> Level designates fine-grained
    77       informational events that are most useful to debug an
    78       application.  */
    79    final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
    80
    81    /***
    82      * The <code>TRACE</code> Level designates finer-grained
    83      * informational events than the <code>DEBUG</code level.
    84     *  @since 1.2.12
    85      */
    86    public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7);
    87
    88
    89    /***
    90       The <code>ALL</code> has the lowest possible rank and is intended to
    91       turn on all logging.  */
    92    final static public Level ALL = new Level(ALL_INT, "ALL", 7);
    93
    94    /***
    95     * Serialization version id.
    96     */
    97    static final long serialVersionUID = 3491141966387921974L;
    98
    99    /***
    100      Instantiate a Level object.
    101    */
    102   protected
    103   Level(int level, String levelStr, int syslogEquivalent) {
    104     super(level, levelStr, syslogEquivalent);
    105   }
    106
    107
    108   /***
    109      Convert the string passed as argument to a level. If the
    110      conversion fails, then this method returns {@link #DEBUG}.
    111   */
    112   public
    113   static
    114   Level toLevel(String sArg) {
    115     return (Level) toLevel(sArg, Level.DEBUG);
    116   }
    117
    118   /***
    119     Convert an integer passed as argument to a level. If the
    120     conversion fails, then this method returns {@link #DEBUG}.
    121
    122   */
    123   public
    124   static
    125   Level toLevel(int val) {
    126     return (Level) toLevel(val, Level.DEBUG);
    127   }
    128
    129   /***
    130     Convert an integer passed as argument to a level. If the
    131     conversion fails, then this method returns the specified default.
    132   */
    133   public
    134   static
    135   Level toLevel(int val, Level defaultLevel) {
    136     switch(val) {
    137     case ALL_INT: return ALL;
    138     case DEBUG_INT: return Level.DEBUG;
    139     case INFO_INT: return Level.INFO;
    140     case WARN_INT: return Level.WARN;
    141     case ERROR_INT: return Level.ERROR;
    142     case FATAL_INT: return Level.FATAL;
    143     case OFF_INT: return OFF;
    144     case TRACE_INT: return Level.TRACE;
    145     default: return defaultLevel;
    146     }
    147   }
    148
    149   /***
    150      Convert the string passed as argument to a level. If the
    151      conversion fails, then this method returns the value of
    152      <code>defaultLevel</code>.
    153   */
    154   public
    155   static
    156   Level toLevel(String sArg, Level defaultLevel) {
    157     if(sArg == null)
    158        return defaultLevel;
    159
    160     String s = sArg.toUpperCase();
    161
    162     if(s.equals("ALL")) return Level.ALL;
    163     if(s.equals("DEBUG")) return Level.DEBUG;
    164     if(s.equals("INFO"))  return Level.INFO;
    165     if(s.equals("WARN"))  return Level.WARN;
    166     if(s.equals("ERROR")) return Level.ERROR;
    167     if(s.equals("FATAL")) return Level.FATAL;
    168     if(s.equals("OFF")) return Level.OFF;
    169     if(s.equals("TRACE")) return Level.TRACE;
    170     //
    171     //   For Turkish i problem, see bug 40937
    172     //
    173     if(s.equals("\u0130NFO")) return Level.INFO;
    174     return defaultLevel;
    175   }
    176
    177     /***
    178      * Custom deserialization of Level.
    179      * @param s serialization stream.
    180      * @throws IOException if IO exception.
    181      * @throws ClassNotFoundException if class not found.
    182      */
    183     private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
    184       s.defaultReadObject();
    185       level = s.readInt();
    186       syslogEquivalent = s.readInt();
    187       levelStr = s.readUTF();
    188       if (levelStr == null) {
    189           levelStr = "";
    190       }
    191     }
    192
    193     /***
    194      * Serialize level.
    195      * @param s serialization stream.
    196      * @throws IOException if exception during serialization.
    197      */
    198     private void writeObject(final ObjectOutputStream s) throws IOException {
    199         s.defaultWriteObject();
    200         s.writeInt(level);
    201         s.writeInt(syslogEquivalent);
    202         s.writeUTF(levelStr);
    203     }
    204
    205     /***
    206      * Resolved deserialized level to one of the stock instances.
    207      * May be overriden in classes derived from Level.
    208      * @return resolved object.
    209      * @throws ObjectStreamException if exception during resolution.
    210      */
    211     private Object readResolve() throws ObjectStreamException {
    212         //
    213         //  if the deserizalized object is exactly an instance of Level
    214         //
    215         if (getClass() == Level.class) {
    216             return toLevel(level);
    217         }
    218         //
    219         //   extension of Level can't substitute stock item
    220         //
    221         return this;
    222     }
    223
    224 }