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 org.apache.log4j.Level;
21  
22  import java.util.ArrayList;
23  import java.util.List;
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 = 22000;
43      /**
44       * Numerical value for WARNING.
45       */
46      public static final int WARNING_INT = 21000;
47  
48      //INFO level defined in parent as 20000..no need to redefine here
49  
50      /**
51       * Numerical value for CONFIG.
52       */
53      public static final int CONFIG_INT = 14000;
54      /**
55       * Numerical value for FINE.
56       */
57      public static final int FINE_INT = 13000;
58      /**
59       * Numerical value for FINER.
60       */
61      public static final int FINER_INT = 12000;
62      /**
63       * Numerical value for FINEST.
64       */
65      public static final int FINEST_INT = 11000;
66      /**
67       * Numerical value for UNKNOWN.
68       */
69      public static final int UNKNOWN_INT = 10000;
70  
71      /**
72       * SEVERE.
73       */
74      public static final UtilLoggingLevel SEVERE =
75          new UtilLoggingLevel(SEVERE_INT, "SEVERE", 0);
76      /**
77       * WARNING.
78       */
79      public static final UtilLoggingLevel WARNING =
80          new UtilLoggingLevel(WARNING_INT, "WARNING", 4);
81      /**
82       * INFO.
83       */
84      //note: we've aligned the int values of the java.util.logging INFO level with log4j's level
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      *
111      * @param level            numeric value for level.
112      * @param levelStr         symbolic name for level.
113      * @param syslogEquivalent Equivalent syslog severity.
114      */
115     protected UtilLoggingLevel(final int level,
116                                final String levelStr,
117                                final int syslogEquivalent) {
118         super(level, levelStr, syslogEquivalent);
119     }
120 
121     /**
122      * Convert an integer passed as argument to a level. If the
123      * conversion fails, then this method returns the specified default.
124      *
125      * @param val          numeric value.
126      * @param defaultLevel level to be returned if no level matches
127      *                     numeric value.
128      * @return matching level or default level.
129      */
130     public static UtilLoggingLevel toLevel(final int val,
131                                            final UtilLoggingLevel defaultLevel) {
132         switch (val) {
133             case SEVERE_INT:
134                 return SEVERE;
135 
136             case WARNING_INT:
137                 return WARNING;
138 
139             case INFO_INT:
140                 return INFO;
141 
142             case CONFIG_INT:
143                 return CONFIG;
144 
145             case FINE_INT:
146                 return FINE;
147 
148             case FINER_INT:
149                 return FINER;
150 
151             case FINEST_INT:
152                 return FINEST;
153 
154             default:
155                 return defaultLevel;
156         }
157     }
158 
159     /**
160      * Gets level matching numeric value.
161      *
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      *
172      * @return list of supported levels.
173      */
174     public static List<UtilLoggingLevel> getAllPossibleLevels() {
175         ArrayList<UtilLoggingLevel> list = new ArrayList<>();
176         list.add(FINE);
177         list.add(FINER);
178         list.add(FINEST);
179         list.add(INFO);
180         list.add(CONFIG);
181         list.add(WARNING);
182         list.add(SEVERE);
183         return list;
184     }
185 
186     /**
187      * Get level with specified symbolic name.
188      *
189      * @param s symbolic name.
190      * @return matching level or Level.DEBUG if no match.
191      */
192     public static Level toLevel(final String s) {
193         return toLevel(s, Level.DEBUG);
194     }
195 
196 
197     /**
198      * Get level with specified symbolic name.
199      *
200      * @param sArg         symbolic name.
201      * @param defaultLevel level to return if no match.
202      * @return matching level or defaultLevel if no match.
203      */
204     public static Level toLevel(final String sArg,
205                                 final Level defaultLevel) {
206         if (sArg == null) {
207             return defaultLevel;
208         }
209 
210         String s = sArg.toUpperCase();
211 
212         if (s.equals("SEVERE")) {
213             return SEVERE;
214         }
215 
216         //if(s.equals("FINE")) return Level.FINE;
217         if (s.equals("WARNING")) {
218             return WARNING;
219         }
220 
221         if (s.equals("INFO")) {
222             return INFO;
223         }
224 
225         if (s.equals("CONFI")) {
226             return CONFIG;
227         }
228 
229         if (s.equals("FINE")) {
230             return FINE;
231         }
232 
233         if (s.equals("FINER")) {
234             return FINER;
235         }
236 
237         if (s.equals("FINEST")) {
238             return FINEST;
239         }
240         return defaultLevel;
241     }
242 
243 }