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.log4j.lf5;
    18
    19  import java.awt.Color;
    20  import java.util.Arrays;
    21  import java.util.HashMap;
    22  import java.util.Iterator;
    23  import java.util.List;
    24  import java.util.Map;
    25
    26  /**
    27   * The LogLevel class defines a set of standard logging levels.
    28   *
    29   * The logging Level objects are ordered and are specified by ordered
    30   * integers. Enabling logging at a given level also enables logging at all
    31   * higher levels.
    32   *
    33   * @author Michael J. Sikorsky
    34   * @author Robert Shaw
    35   * @author Brent Sprecher
    36   * @author Richard Hurst
    37   * @author Brad Marlborough
    38   */
    39
    40  // Contributed by ThoughtWorks Inc.
    41
    42  public class LogLevel implements java.io.Serializable {
    43    //--------------------------------------------------------------------------
    44    //   Constants:
    45    //--------------------------------------------------------------------------
    46
    47    // log4j log levels.
    48    public final static LogLevel FATAL = new LogLevel("FATAL", 0);
    49    public final static LogLevel ERROR = new LogLevel("ERROR", 1);
    50    public final static LogLevel WARN = new LogLevel("WARN", 2);
    51    public final static LogLevel INFO = new LogLevel("INFO", 3);
    52    public final static LogLevel DEBUG = new LogLevel("DEBUG", 4);
    53
    54    // jdk1.4 log levels NOTE: also includes INFO
    55    public final static LogLevel SEVERE = new LogLevel("SEVERE", 1);
    56    public final static LogLevel WARNING = new LogLevel("WARNING", 2);
    57    public final static LogLevel CONFIG = new LogLevel("CONFIG", 4);
    58    public final static LogLevel FINE = new LogLevel("FINE", 5);
    59    public final static LogLevel FINER = new LogLevel("FINER", 6);
    60    public final static LogLevel FINEST = new LogLevel("FINEST", 7);
    61
    62    //--------------------------------------------------------------------------
    63    //   Protected Variables:
    64    //--------------------------------------------------------------------------
    65    protected String _label;
    66    protected int _precedence;
    67    //--------------------------------------------------------------------------
    68    //   Private Variables:
    69    //--------------------------------------------------------------------------
    70    private static LogLevel[] _log4JLevels;
    71    private static LogLevel[] _jdk14Levels;
    72    private static LogLevel[] _allDefaultLevels;
    73    private static Map _logLevelMap;
    74    private static Map _logLevelColorMap;
    75    private static Map _registeredLogLevelMap = new HashMap();
    76
    77    //--------------------------------------------------------------------------
    78    //   Constructors:
    79    //--------------------------------------------------------------------------
    80    static {
    81      _log4JLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG};
    82      _jdk14Levels = new LogLevel[]{SEVERE, WARNING, INFO,
    83                                    CONFIG, FINE, FINER, FINEST};
    84      _allDefaultLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG,
    85                                         SEVERE, WARNING, CONFIG, FINE, FINER, FINEST};
    86
    87      _logLevelMap = new HashMap();
    88      for (int i = 0; i < _allDefaultLevels.length; i++) {
    89        _logLevelMap.put(_allDefaultLevels[i].getLabel(), _allDefaultLevels[i]);
    90      }
    91
    92      // prepopulate map with levels and text color of black
    93      _logLevelColorMap = new HashMap();
    94      for (int i = 0; i < _allDefaultLevels.length; i++) {
    95        _logLevelColorMap.put(_allDefaultLevels[i], Color.black);
    96      }
    97    }
    98
    99    public LogLevel(String label, int precedence) {
    100     _label = label;
    101     _precedence = precedence;
    102   }
    103
    104   //--------------------------------------------------------------------------
    105   //   Public Methods:
    106   //--------------------------------------------------------------------------
    107
    108   /**
    109    * Return the Label of the LogLevel.
    110    */
    111   public String getLabel() {
    112     return _label;
    113   }
    114
    115   /**
    116    * Returns true if the level supplied is encompassed by this level.
    117    * For example, LogLevel.SEVERE encompasses no other LogLevels and
    118    * LogLevel.FINE encompasses all other LogLevels.  By definition,
    119    * a LogLevel encompasses itself.
    120    */
    121   public boolean encompasses(LogLevel level) {
    122     if (level.getPrecedence() <= getPrecedence()) {
    123       return true;
    124     }
    125
    126     return false;
    127   }
    128
    129   /**
    130    * Convert a log level label into a LogLevel object.
    131    *
    132    * @param level The label of a level to be converted into a LogLevel.
    133    * @return LogLevel The LogLevel with a label equal to level.
    134    * @throws LogLevelFormatException Is thrown when the level can not be
    135    *         converted into a LogLevel.
    136    */
    137   public static LogLevel valueOf(String level)
    138       throws LogLevelFormatException {
    139     LogLevel logLevel = null;
    140     if (level != null) {
    141       level = level.trim().toUpperCase();
    142       logLevel = (LogLevel) _logLevelMap.get(level);
    143     }
    144
    145     // Didn't match, Check for registered LogLevels
    146     if (logLevel == null && _registeredLogLevelMap.size() > 0) {
    147       logLevel = (LogLevel) _registeredLogLevelMap.get(level);
    148     }
    149
    150     if (logLevel == null) {
    151       StringBuffer buf = new StringBuffer();
    152       buf.append("Error while trying to parse (" + level + ") into");
    153       buf.append(" a LogLevel.");
    154       throw new LogLevelFormatException(buf.toString());
    155     }
    156     return logLevel;
    157   }
    158
    159   /**
    160    * Registers a used defined LogLevel.
    161    *
    162    * @param logLevel The log level to be registered. Cannot be a default LogLevel
    163    * @return LogLevel The replaced log level.
    164    */
    165   public static LogLevel register(LogLevel logLevel) {
    166     if (logLevel == null) return null;
    167
    168     // ensure that this is not a default log level
    169     if (_logLevelMap.get(logLevel.getLabel()) == null) {
    170       return (LogLevel) _registeredLogLevelMap.put(logLevel.getLabel(), logLevel);
    171     }
    172
    173     return null;
    174   }
    175
    176   public static void register(LogLevel[] logLevels) {
    177     if (logLevels != null) {
    178       for (int i = 0; i < logLevels.length; i++) {
    179         register(logLevels[i]);
    180       }
    181     }
    182   }
    183
    184   public static void register(List logLevels) {
    185     if (logLevels != null) {
    186       Iterator it = logLevels.iterator();
    187       while (it.hasNext()) {
    188         register((LogLevel) it.next());
    189       }
    190     }
    191   }
    192
    193   public boolean equals(Object o) {
    194     boolean equals = false;
    195
    196     if (o instanceof LogLevel) {
    197       if (this.getPrecedence() ==
    198           ((LogLevel) o).getPrecedence()) {
    199         equals = true;
    200       }
    201
    202     }
    203
    204     return equals;
    205   }
    206
    207   public int hashCode() {
    208     return _label.hashCode();
    209   }
    210
    211   public String toString() {
    212     return _label;
    213   }
    214
    215   // set a text color for a specific log level
    216   public void setLogLevelColorMap(LogLevel level, Color color) {
    217     // remove the old entry
    218     _logLevelColorMap.remove(level);
    219     // add the new color entry
    220     if (color == null) {
    221       color = Color.black;
    222     }
    223     _logLevelColorMap.put(level, color);
    224   }
    225
    226   public static void resetLogLevelColorMap() {
    227     // empty the map
    228     _logLevelColorMap.clear();
    229
    230     // repopulate map and reset text color black
    231     for (int i = 0; i < _allDefaultLevels.length; i++) {
    232       _logLevelColorMap.put(_allDefaultLevels[i], Color.black);
    233     }
    234   }
    235
    236   /**
    237    * @return A <code>List</code> of <code>LogLevel</code> objects that map
    238    * to log4j <code>Priority</code> objects.
    239    */
    240   public static List getLog4JLevels() {
    241     return Arrays.asList(_log4JLevels);
    242   }
    243
    244   public static List getJdk14Levels() {
    245     return Arrays.asList(_jdk14Levels);
    246   }
    247
    248   public static List getAllDefaultLevels() {
    249     return Arrays.asList(_allDefaultLevels);
    250   }
    251
    252   public static Map getLogLevelColorMap() {
    253     return _logLevelColorMap;
    254   }
    255
    256   //--------------------------------------------------------------------------
    257   //   Protected Methods:
    258   //--------------------------------------------------------------------------
    259
    260   protected int getPrecedence() {
    261     return _precedence;
    262   }
    263
    264   //--------------------------------------------------------------------------
    265   //   Private Methods:
    266   //--------------------------------------------------------------------------
    267
    268   //--------------------------------------------------------------------------
    269   //   Nested Top-Level Classes or Interfaces:
    270   //--------------------------------------------------------------------------
    271
    272 }
    273
    274
    275
    276
    277
    278