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