001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.log4j.Level;
024
025/**
026 * An extension of the Level class that provides support for java.util.logging
027 * Levels.
028 *
029 *
030 * @author Scott Deboy (sdeboy@apache.org)
031 */
032
033public class UtilLoggingLevel extends Level {
034
035    /**
036     * Serialization version id.
037     */
038    private static final long serialVersionUID = 909301162611820211L;
039
040    /**
041     * Numerical value for SEVERE.
042     */
043    public static final int SEVERE_INT = 22000;
044    /**
045     * Numerical value for WARNING.
046     */
047    public static final int WARNING_INT = 21000;
048
049    //INFO level defined in parent as 20000..no need to redefine here
050    
051    /**
052     * Numerical value for CONFIG.
053     */
054    public static final int CONFIG_INT = 14000;
055    /**
056     * Numerical value for FINE.
057     */
058    public static final int FINE_INT = 13000;
059    /**
060     * Numerical value for FINER.
061     */
062    public static final int FINER_INT = 12000;
063    /**
064     * Numerical value for FINEST.
065     */
066    public static final int FINEST_INT = 11000;
067    /**
068     * Numerical value for UNKNOWN.
069     */
070    public static final int UNKNOWN_INT = 10000;
071
072    /**
073     * SEVERE.
074     */
075    public static final UtilLoggingLevel SEVERE =
076            new UtilLoggingLevel(SEVERE_INT, "SEVERE", 0);
077    /**
078     * WARNING.
079     */
080    public static final UtilLoggingLevel WARNING =
081            new UtilLoggingLevel(WARNING_INT, "WARNING", 4);
082    /**
083     * INFO.
084     */
085    //note: we've aligned the int values of the java.util.logging INFO level with log4j's level
086    public static final UtilLoggingLevel INFO =
087            new UtilLoggingLevel(INFO_INT, "INFO", 5);
088    /**
089     * CONFIG.
090     */
091    public static final UtilLoggingLevel CONFIG =
092            new UtilLoggingLevel(CONFIG_INT, "CONFIG", 6);
093    /**
094     * FINE.
095     */
096    public static final UtilLoggingLevel FINE =
097            new UtilLoggingLevel(FINE_INT, "FINE", 7);
098    /**
099     * FINER.
100     */
101    public static final UtilLoggingLevel FINER =
102            new UtilLoggingLevel(FINER_INT, "FINER", 8);
103    /**
104     * FINEST.
105     */
106    public static final UtilLoggingLevel FINEST =
107            new UtilLoggingLevel(FINEST_INT, "FINEST", 9);
108
109    /**
110     * Create new instance.
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     * @param val numeric value.
125     * @param defaultLevel level to be returned if no level matches
126     * numeric value.
127     * @return matching level or default level.
128     */
129    public static UtilLoggingLevel toLevel(final int val,
130                               final UtilLoggingLevel defaultLevel) {
131        switch (val) {
132            case SEVERE_INT:
133                return SEVERE;
134
135            case WARNING_INT:
136                return WARNING;
137
138            case INFO_INT:
139                return INFO;
140
141            case CONFIG_INT:
142                return CONFIG;
143
144            case FINE_INT:
145                return FINE;
146
147            case FINER_INT:
148                return FINER;
149
150            case FINEST_INT:
151                return FINEST;
152
153            default:
154                return defaultLevel;
155        }
156    }
157
158    /**
159     * Gets level matching numeric value.
160     * @param val numeric value.
161     * @return  matching level or UtilLoggerLevel.FINEST if no match.
162     */
163    public static Level toLevel(final int val) {
164        return toLevel(val, FINEST);
165    }
166
167    /**
168     * Gets list of supported levels.
169     * @return  list of supported levels.
170     */
171    public static List getAllPossibleLevels() {
172        ArrayList list = new ArrayList();
173        list.add(FINE);
174        list.add(FINER);
175        list.add(FINEST);
176        list.add(INFO);
177        list.add(CONFIG);
178        list.add(WARNING);
179        list.add(SEVERE);
180        return list;
181    }
182
183    /**
184     * Get level with specified symbolic name.
185     * @param s symbolic name.
186     * @return matching level or Level.DEBUG if no match.
187     */
188    public static Level toLevel(final String s) {
189        return toLevel(s, Level.DEBUG);
190    }
191
192
193    /**
194     * Get level with specified symbolic name.
195     * @param sArg symbolic name.
196     * @param defaultLevel level to return if no match.
197     * @return matching level or defaultLevel if no match.
198     */
199    public static Level toLevel(final String sArg,
200                                final Level defaultLevel) {
201        if (sArg == null) {
202            return defaultLevel;
203        }
204
205        String s = sArg.toUpperCase();
206
207        if (s.equals("SEVERE")) {
208            return SEVERE;
209        }
210
211        //if(s.equals("FINE")) return Level.FINE;
212        if (s.equals("WARNING")) {
213            return WARNING;
214        }
215
216        if (s.equals("INFO")) {
217            return INFO;
218        }
219
220        if (s.equals("CONFI")) {
221            return CONFIG;
222        }
223
224        if (s.equals("FINE")) {
225            return FINE;
226        }
227
228        if (s.equals("FINER")) {
229            return FINER;
230        }
231
232        if (s.equals("FINEST")) {
233            return FINEST;
234        }
235        return defaultLevel;
236    }
237
238}