UtilLoggingLevel xref

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  package org.apache.log4j.helpers;
    19
    20  import java.util.ArrayList;
    21  import java.util.List;
    22
    23  import org.apache.log4j.Level;
    24
    25  /**
    26   * An extension of the Level class that provides support for java.util.logging
    27   * Levels.
    28   *
    29   *
    30   * @author Scott Deboy (sdeboy@apache.org)
    31   */
    32
    33  public class UtilLoggingLevel extends Level {
    34
    35      /**
    36       * Serialization version id.
    37       */
    38      private static final long serialVersionUID = 909301162611820211L;
    39
    40      /**
    41       * Numerical value for SEVERE.
    42       */
    43      public static final int SEVERE_INT = 22000;
    44      /**
    45       * Numerical value for WARNING.
    46       */
    47      public static final int WARNING_INT = 21000;
    48
    49      //INFO level defined in parent as 20000..no need to redefine here
    50
    51      /**
    52       * Numerical value for CONFIG.
    53       */
    54      public static final int CONFIG_INT = 14000;
    55      /**
    56       * Numerical value for FINE.
    57       */
    58      public static final int FINE_INT = 13000;
    59      /**
    60       * Numerical value for FINER.
    61       */
    62      public static final int FINER_INT = 12000;
    63      /**
    64       * Numerical value for FINEST.
    65       */
    66      public static final int FINEST_INT = 11000;
    67      /**
    68       * Numerical value for UNKNOWN.
    69       */
    70      public static final int UNKNOWN_INT = 10000;
    71
    72      /**
    73       * SEVERE.
    74       */
    75      public static final UtilLoggingLevel SEVERE =
    76              new UtilLoggingLevel(SEVERE_INT, "SEVERE", 0);
    77      /**
    78       * WARNING.
    79       */
    80      public static final UtilLoggingLevel WARNING =
    81              new UtilLoggingLevel(WARNING_INT, "WARNING", 4);
    82      /**
    83       * INFO.
    84       */
    85      //note: we've aligned the int values of the java.util.logging INFO level with log4j's level
    86      public static final UtilLoggingLevel INFO =
    87              new UtilLoggingLevel(INFO_INT, "INFO", 5);
    88      /**
    89       * CONFIG.
    90       */
    91      public static final UtilLoggingLevel CONFIG =
    92              new UtilLoggingLevel(CONFIG_INT, "CONFIG", 6);
    93      /**
    94       * FINE.
    95       */
    96      public static final UtilLoggingLevel FINE =
    97              new UtilLoggingLevel(FINE_INT, "FINE", 7);
    98      /**
    99       * FINER.
    100      */
    101     public static final UtilLoggingLevel FINER =
    102             new UtilLoggingLevel(FINER_INT, "FINER", 8);
    103     /**
    104      * FINEST.
    105      */
    106     public static final UtilLoggingLevel FINEST =
    107             new UtilLoggingLevel(FINEST_INT, "FINEST", 9);
    108
    109     /**
    110      * Create new instance.
    111      * @param level numeric value for level.
    112      * @param levelStr symbolic name for level.
    113      * @param syslogEquivalent Equivalent syslog severity.
    114      */
    115     protected UtilLoggingLevel(final int level,
    116                                final String levelStr,
    117                                final int syslogEquivalent) {
    118         super(level, levelStr, syslogEquivalent);
    119     }
    120
    121     /**
    122      * Convert an integer passed as argument to a level. If the
    123      * conversion fails, then this method returns the specified default.
    124      * @param val numeric value.
    125      * @param defaultLevel level to be returned if no level matches
    126      * numeric value.
    127      * @return matching level or default level.
    128      */
    129     public static UtilLoggingLevel toLevel(final int val,
    130                                final UtilLoggingLevel defaultLevel) {
    131         switch (val) {
    132             case SEVERE_INT:
    133                 return SEVERE;
    134
    135             case WARNING_INT:
    136                 return WARNING;
    137
    138             case INFO_INT:
    139                 return INFO;
    140
    141             case CONFIG_INT:
    142                 return CONFIG;
    143
    144             case FINE_INT:
    145                 return FINE;
    146
    147             case FINER_INT:
    148                 return FINER;
    149
    150             case FINEST_INT:
    151                 return FINEST;
    152
    153             default:
    154                 return defaultLevel;
    155         }
    156     }
    157
    158     /**
    159      * Gets level matching numeric value.
    160      * @param val numeric value.
    161      * @return  matching level or UtilLoggerLevel.FINEST if no match.
    162      */
    163     public static Level toLevel(final int val) {
    164         return toLevel(val, FINEST);
    165     }
    166
    167     /**
    168      * Gets list of supported levels.
    169      * @return  list of supported levels.
    170      */
    171     public static List getAllPossibleLevels() {
    172         ArrayList list = new ArrayList();
    173         list.add(FINE);
    174         list.add(FINER);
    175         list.add(FINEST);
    176         list.add(INFO);
    177         list.add(CONFIG);
    178         list.add(WARNING);
    179         list.add(SEVERE);
    180         return list;
    181     }
    182
    183     /**
    184      * Get level with specified symbolic name.
    185      * @param s symbolic name.
    186      * @return matching level or Level.DEBUG if no match.
    187      */
    188     public static Level toLevel(final String s) {
    189         return toLevel(s, Level.DEBUG);
    190     }
    191
    192
    193     /**
    194      * Get level with specified symbolic name.
    195      * @param sArg symbolic name.
    196      * @param defaultLevel level to return if no match.
    197      * @return matching level or defaultLevel if no match.
    198      */
    199     public static Level toLevel(final String sArg,
    200                                 final Level defaultLevel) {
    201         if (sArg == null) {
    202             return defaultLevel;
    203         }
    204
    205         String s = sArg.toUpperCase();
    206
    207         if (s.equals("SEVERE")) {
    208             return SEVERE;
    209         }
    210
    211         //if(s.equals("FINE")) return Level.FINE;
    212         if (s.equals("WARNING")) {
    213             return WARNING;
    214         }
    215
    216         if (s.equals("INFO")) {
    217             return INFO;
    218         }
    219
    220         if (s.equals("CONFI")) {
    221             return CONFIG;
    222         }
    223
    224         if (s.equals("FINE")) {
    225             return FINE;
    226         }
    227
    228         if (s.equals("FINER")) {
    229             return FINER;
    230         }
    231
    232         if (s.equals("FINEST")) {
    233             return FINEST;
    234         }
    235         return defaultLevel;
    236     }
    237
    238 }