KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > DynamicLogger


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging;
23
24 /**
25  * An extension of the JBoss Logger that adds a log()
26  * primitive that maps to a dynamically defined log level.
27  *
28  * TODO - Make sure serialization works correctly
29  *
30  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
31  * @version <tt>$Revision: 2081 $</tt>
32  *
33  * @since 4.0.3
34  */

35 public class DynamicLogger extends Logger
36 {
37    /** The serialVersionUID */
38    private static final long serialVersionUID = -5963699806863917370L;
39    
40    /** No logging */
41    public static final int LOG_LEVEL_NONE = 0;
42
43    /** Fatal level logging */
44    public static final int LOG_LEVEL_FATAL = 1;
45
46    /** Error level logging */
47    public static final int LOG_LEVEL_ERROR = 2;
48
49    /** Warn level logging */
50    public static final int LOG_LEVEL_WARN = 3;
51
52    /** Info level logging */
53    public static final int LOG_LEVEL_INFO = 4;
54    
55    /** Debug level logging */
56    public static final int LOG_LEVEL_DEBUG = 5;
57
58    /** Trace level logging */
59    public static final int LOG_LEVEL_TRACE = 6;
60    
61    /** The available log level strings */
62    public final static String JavaDoc[] LOG_LEVEL_STRINGS =
63       { "NONE", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
64     
65    /** The log level to use for the "log" primitive */
66    private int logLevel = LOG_LEVEL_DEBUG;
67
68    /**
69     * Create a DynamicLogger instance given the logger name.
70     *
71     * @param name the logger name
72     * @return the dynamic logger
73     */

74    public static DynamicLogger getDynamicLogger(String JavaDoc name)
75    {
76       return new DynamicLogger(name);
77    }
78
79    /**
80     * Create a DynamicLogger instance given the logger name with the given suffix.
81     *
82     * <p>This will include a logger seperator between classname and suffix
83     *
84     * @param name The logger name
85     * @param suffix A suffix to append to the classname.
86     * @return the dynamic logger
87     */

88    public static DynamicLogger getDynamicLogger(String JavaDoc name, String JavaDoc suffix)
89    {
90       return new DynamicLogger(name + "." + suffix);
91    }
92
93    /**
94     * Create a DynamicLogger instance given the logger class. This simply
95     * calls create(clazz.getName()).
96     *
97     * @param clazz the Class whose name will be used as the logger name
98     * @return the dynamic logger
99     */

100    public static DynamicLogger getDynamicLogger(Class JavaDoc clazz)
101    {
102       return new DynamicLogger(clazz.getName());
103    }
104
105    /**
106     * Create a DynamicLogger instance given the logger class with the given suffix.
107     *
108     * <p>This will include a logger seperator between classname and suffix
109     *
110     * @param clazz The Class whose name will be used as the logger name.
111     * @param suffix A suffix to append to the classname.
112     * @return the dynamic logger
113     */

114    public static DynamicLogger getDynamicLogger(Class JavaDoc clazz, String JavaDoc suffix)
115    {
116       return new DynamicLogger(clazz.getName() + "." + suffix);
117    }
118
119    /**
120     * Create a new DynamicLogger.
121     *
122     * @param name the log name
123     */

124    protected DynamicLogger(final String JavaDoc name)
125    {
126       super(name);
127    }
128    
129    /**
130     * Sets the logLevel for the log() primitive
131     *
132     * @param logLevel between LOG_LEVEL_NONE and LOG_LEVEL_TRACE
133     */

134    public void setLogLevel(int logLevel)
135    {
136       if (logLevel >= LOG_LEVEL_NONE && logLevel <= LOG_LEVEL_TRACE)
137       {
138          this.logLevel = logLevel;
139       }
140    }
141    
142    /**
143     * Gets the logLevel of the log() primitive
144     *
145     * @return the logLevel of the log() primitive
146     */

147    public int getLogLevel()
148    {
149       return logLevel;
150    }
151    
152    /**
153     * Sets the logLevel of the log() primitive
154     *
155     * @param logLevelString the log level in String form
156     */

157    public void setLogLevelAsString(String JavaDoc logLevelString)
158    {
159       if (logLevelString != null)
160       {
161          logLevelString = logLevelString.toUpperCase().trim();
162          
163          for (int i = 0; i <= LOG_LEVEL_TRACE; i++)
164          {
165             if (logLevelString.equals(LOG_LEVEL_STRINGS[i]))
166             {
167                // match
168
this.logLevel = i;
169                break;
170             }
171          }
172       }
173    }
174    
175    /**
176     * Gets the logLevel of the log() primitive in String form
177     *
178     * @return the logLevel of the log() primitive in String form
179     */

180    public String JavaDoc getLogLevelAsString()
181    {
182       return LOG_LEVEL_STRINGS[logLevel];
183    }
184    
185    /**
186     * Logs a message using dynamic log level
187     *
188     * @param message the message to log
189     */

190    public void log(Object JavaDoc message)
191    {
192       switch (logLevel)
193       {
194          case LOG_LEVEL_TRACE:
195             super.trace(message);
196             break;
197             
198          case LOG_LEVEL_DEBUG:
199             super.debug(message);
200             break;
201             
202          case LOG_LEVEL_INFO:
203             super.info(message);
204             break;
205             
206          case LOG_LEVEL_WARN:
207             super.warn(message);
208             break;
209             
210          case LOG_LEVEL_ERROR:
211             super.error(message);
212             break;
213             
214          case LOG_LEVEL_FATAL:
215             super.fatal(message);
216             break;
217             
218          case LOG_LEVEL_NONE:
219          default:
220             // do nothing
221
break;
222       }
223    }
224    
225    /**
226     * Logs a message and a throwable using dynamic log level
227     *
228     * @param message the message to log
229     * @param t the throwable to log
230     */

231    public void log(Object JavaDoc message, Throwable JavaDoc t)
232    {
233       switch (logLevel)
234       {
235          case LOG_LEVEL_TRACE:
236             super.trace(message, t);
237             break;
238             
239          case LOG_LEVEL_DEBUG:
240             super.debug(message, t);
241             break;
242             
243          case LOG_LEVEL_INFO:
244             super.info(message, t);
245             break;
246             
247          case LOG_LEVEL_WARN:
248             super.warn(message, t);
249             break;
250             
251          case LOG_LEVEL_ERROR:
252             super.error(message, t);
253             break;
254             
255          case LOG_LEVEL_FATAL:
256             super.fatal(message, t);
257             break;
258             
259          case LOG_LEVEL_NONE:
260          default:
261             // do nothing
262
break;
263       }
264    }
265 }
266
Popular Tags