KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > customLevel > XLevel


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
17 package examples.customLevel;
18
19 import org.apache.log4j.Level;
20 import org.apache.log4j.Priority;
21
22
23 /**
24    This class introduces a new level level called TRACE. TRACE has
25    lower level than DEBUG.
26
27  */

28 public class XLevel extends Level {
29
30   static public final int TRACE_INT = Level.DEBUG_INT - 1;
31   static public final int LETHAL_INT = Level.FATAL_INT + 1;
32
33
34   private static String JavaDoc TRACE_STR = "TRACE";
35   private static String JavaDoc LETHAL_STR = "LETHAL";
36
37
38   public static final XLevel TRACE = new XLevel(TRACE_INT, TRACE_STR, 7);
39   public static final XLevel LETHAL = new XLevel(LETHAL_INT, LETHAL_STR,
40                                0);
41
42
43   protected
44   XLevel(int level, String JavaDoc strLevel, int syslogEquiv) {
45     super(level, strLevel, syslogEquiv);
46   }
47
48   /**
49      Convert the string passed as argument to a level. If the
50      conversion fails, then this method returns {@link #TRACE}.
51   */

52   public
53   static
54   Level toLevel(String JavaDoc sArg) {
55     return (Level) toLevel(sArg, XLevel.TRACE);
56   }
57
58
59   public
60   static
61   Level toLevel(String JavaDoc sArg, Level defaultValue) {
62
63     if(sArg == null) {
64       return defaultValue;
65     }
66     String JavaDoc stringVal = sArg.toUpperCase();
67     
68     if(stringVal.equals(TRACE_STR)) {
69       return XLevel.TRACE;
70     } else if(stringVal.equals(LETHAL_STR)) {
71       return XLevel.LETHAL;
72     }
73       
74     return Level.toLevel(sArg, (Level) defaultValue);
75   }
76
77
78   public
79   static
80   Level toLevel(int i) throws IllegalArgumentException JavaDoc {
81     switch(i) {
82     case TRACE_INT: return XLevel.TRACE;
83     case LETHAL_INT: return XLevel.LETHAL;
84     }
85     return Level.toLevel(i);
86   }
87
88 }
89   
90
Popular Tags