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