KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > LogLevel


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5;
17
18 import java.awt.*;
19 import java.util.*;
20 import java.util.List JavaDoc;
21
22 /**
23  * The LogLevel class defines a set of standard logging levels.
24  *
25  * The logging Level objects are ordered and are specified by ordered
26  * integers. Enabling logging at a given level also enables logging at all
27  * higher levels.
28  *
29  * @author Michael J. Sikorsky
30  * @author Robert Shaw
31  * @author Brent Sprecher
32  * @author Richard Hurst
33  * @author Brad Marlborough
34  */

35
36 // Contributed by ThoughtWorks Inc.
37

38 public class LogLevel implements java.io.Serializable JavaDoc {
39   //--------------------------------------------------------------------------
40
// Constants:
41
//--------------------------------------------------------------------------
42

43   // log4j log levels.
44
public final static LogLevel FATAL = new LogLevel("FATAL", 0);
45   public final static LogLevel ERROR = new LogLevel("ERROR", 1);
46   public final static LogLevel WARN = new LogLevel("WARN", 2);
47   public final static LogLevel INFO = new LogLevel("INFO", 3);
48   public final static LogLevel DEBUG = new LogLevel("DEBUG", 4);
49
50   // jdk1.4 log levels NOTE: also includes INFO
51
public final static LogLevel SEVERE = new LogLevel("SEVERE", 1);
52   public final static LogLevel WARNING = new LogLevel("WARNING", 2);
53   public final static LogLevel CONFIG = new LogLevel("CONFIG", 4);
54   public final static LogLevel FINE = new LogLevel("FINE", 5);
55   public final static LogLevel FINER = new LogLevel("FINER", 6);
56   public final static LogLevel FINEST = new LogLevel("FINEST", 7);
57
58   //--------------------------------------------------------------------------
59
// Protected Variables:
60
//--------------------------------------------------------------------------
61
protected String JavaDoc _label;
62   protected int _precedence;
63   //--------------------------------------------------------------------------
64
// Private Variables:
65
//--------------------------------------------------------------------------
66
private static LogLevel[] _log4JLevels;
67   private static LogLevel[] _jdk14Levels;
68   private static LogLevel[] _allDefaultLevels;
69   private static Map _logLevelMap;
70   private static Map _logLevelColorMap;
71   private static Map _registeredLogLevelMap = new HashMap();
72
73   //--------------------------------------------------------------------------
74
// Constructors:
75
//--------------------------------------------------------------------------
76
static {
77     _log4JLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG};
78     _jdk14Levels = new LogLevel[]{SEVERE, WARNING, INFO,
79                                   CONFIG, FINE, FINER, FINEST};
80     _allDefaultLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG,
81                                        SEVERE, WARNING, CONFIG, FINE, FINER, FINEST};
82
83     _logLevelMap = new HashMap();
84     for (int i = 0; i < _allDefaultLevels.length; i++) {
85       _logLevelMap.put(_allDefaultLevels[i].getLabel(), _allDefaultLevels[i]);
86     }
87
88     // prepopulate map with levels and text color of black
89
_logLevelColorMap = new HashMap();
90     for (int i = 0; i < _allDefaultLevels.length; i++) {
91       _logLevelColorMap.put(_allDefaultLevels[i], Color.black);
92     }
93   }
94
95   public LogLevel(String JavaDoc label, int precedence) {
96     _label = label;
97     _precedence = precedence;
98   }
99
100   //--------------------------------------------------------------------------
101
// Public Methods:
102
//--------------------------------------------------------------------------
103

104   /**
105    * Return the Label of the LogLevel.
106    */

107   public String JavaDoc getLabel() {
108     return _label;
109   }
110
111   /**
112    * Returns true if the level supplied is encompassed by this level.
113    * For example, LogLevel.SEVERE encompasses no other LogLevels and
114    * LogLevel.FINE encompasses all other LogLevels. By definition,
115    * a LogLevel encompasses itself.
116    */

117   public boolean encompasses(LogLevel level) {
118     if (level.getPrecedence() <= getPrecedence()) {
119       return true;
120     }
121
122     return false;
123   }
124
125   /**
126    * Convert a log level label into a LogLevel object.
127    *
128    * @param level The label of a level to be converted into a LogLevel.
129    * @return LogLevel The LogLevel with a label equal to level.
130    * @throws LogLevelFormatException Is thrown when the level can not be
131    * converted into a LogLevel.
132    */

133   public static LogLevel valueOf(String JavaDoc level)
134       throws LogLevelFormatException {
135     LogLevel logLevel = null;
136     if (level != null) {
137       level = level.trim().toUpperCase();
138       logLevel = (LogLevel) _logLevelMap.get(level);
139     }
140
141     // Didn't match, Check for registered LogLevels
142
if (logLevel == null && _registeredLogLevelMap.size() > 0) {
143       logLevel = (LogLevel) _registeredLogLevelMap.get(level);
144     }
145
146     if (logLevel == null) {
147       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148       buf.append("Error while trying to parse (" + level + ") into");
149       buf.append(" a LogLevel.");
150       throw new LogLevelFormatException(buf.toString());
151     }
152     return logLevel;
153   }
154
155   /**
156    * Registers a used defined LogLevel.
157    *
158    * @param logLevel The log level to be registered. Cannot be a default LogLevel
159    * @return LogLevel The replaced log level.
160    */

161   public static LogLevel register(LogLevel logLevel) {
162     if (logLevel == null) return null;
163
164     // ensure that this is not a default log level
165
if (_logLevelMap.get(logLevel.getLabel()) == null) {
166       return (LogLevel) _registeredLogLevelMap.put(logLevel.getLabel(), logLevel);
167     }
168
169     return null;
170   }
171
172   public static void register(LogLevel[] logLevels) {
173     if (logLevels != null) {
174       for (int i = 0; i < logLevels.length; i++) {
175         register(logLevels[i]);
176       }
177     }
178   }
179
180   public static void register(List JavaDoc logLevels) {
181     if (logLevels != null) {
182       Iterator it = logLevels.iterator();
183       while (it.hasNext()) {
184         register((LogLevel) it.next());
185       }
186     }
187   }
188
189   public boolean equals(Object JavaDoc o) {
190     boolean equals = false;
191
192     if (o instanceof LogLevel) {
193       if (this.getPrecedence() ==
194           ((LogLevel) o).getPrecedence()) {
195         equals = true;
196       }
197
198     }
199
200     return equals;
201   }
202
203   public int hashCode() {
204     return _label.hashCode();
205   }
206
207   public String JavaDoc toString() {
208     return _label;
209   }
210
211   // set a text color for a specific log level
212
public void setLogLevelColorMap(LogLevel level, Color color) {
213     // remove the old entry
214
_logLevelColorMap.remove(level);
215     // add the new color entry
216
if (color == null) {
217       color = Color.black;
218     }
219     _logLevelColorMap.put(level, color);
220   }
221
222   public static void resetLogLevelColorMap() {
223     // empty the map
224
_logLevelColorMap.clear();
225
226     // repopulate map and reset text color black
227
for (int i = 0; i < _allDefaultLevels.length; i++) {
228       _logLevelColorMap.put(_allDefaultLevels[i], Color.black);
229     }
230   }
231
232   /**
233    * @return A <code>List</code> of <code>LogLevel</code> objects that map
234    * to log4j <code>Priority</code> objects.
235    */

236   public static List JavaDoc getLog4JLevels() {
237     return Arrays.asList(_log4JLevels);
238   }
239
240   public static List JavaDoc getJdk14Levels() {
241     return Arrays.asList(_jdk14Levels);
242   }
243
244   public static List JavaDoc getAllDefaultLevels() {
245     return Arrays.asList(_allDefaultLevels);
246   }
247
248   public static Map getLogLevelColorMap() {
249     return _logLevelColorMap;
250   }
251
252   //--------------------------------------------------------------------------
253
// Protected Methods:
254
//--------------------------------------------------------------------------
255

256   protected int getPrecedence() {
257     return _precedence;
258   }
259
260   //--------------------------------------------------------------------------
261
// Private Methods:
262
//--------------------------------------------------------------------------
263

264   //--------------------------------------------------------------------------
265
// Nested Top-Level Classes or Interfaces:
266
//--------------------------------------------------------------------------
267

268 }
269
270
271
272
273
274
275
Popular Tags