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