KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > log > Logger


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.log;
54
55 import java.util.HashMap JavaDoc;
56 import java.util.Map JavaDoc;
57
58 import freemarker.template.utility.ClassUtil;
59
60 /**
61  * The FreeMarker logging facility. This is a polymorphic implementation
62  * that will use whatever logging package it can find on the system:
63  * Apache Jakarta Log4J, Apache Jakarta Avalon LogKit, JDK1.4 logging
64  * (in this order). If it fails to find any of the above, logging will
65  * be suppressed and a short notice output to System.err. You can use the
66  * {@link #selectLoggerLibrary(int)} static method to force use of a specific
67  * logger package, or to turn off logging.
68  * @version $Id: Logger.java,v 1.24 2003/10/03 15:35:10 stephanmueller Exp $
69  * @author Attila Szegedi
70  */

71 public abstract class Logger
72 {
73     /**
74      * Constant used with {@link #selectLoggerLibrary(int)} that indicates the
75      * engine should automatically lookup and use any available logger library.
76      */

77     public static final int LIBRARY_AUTO = -1;
78     /**
79      * Constant used with {@link #selectLoggerLibrary(int)} that indicates the
80      * engine should use no logger package (i.e. turn off logging).
81      */

82     public static final int LIBRARY_NONE = 0;
83     /**
84      * Constant used with {@link #selectLoggerLibrary(int)} that indicates the
85      * engine should use the <tt>java.util.logging</tt> logger package.
86      */

87     public static final int LIBRARY_JAVA = 1;
88     /**
89      * Constant used with {@link #selectLoggerLibrary(int)} that indicates the
90      * engine should use the Apache Jakarta Avalon LogKit logger package.
91      */

92     public static final int LIBRARY_AVALON = 2;
93     /**
94      * Constant used with {@link #selectLoggerLibrary(int)} that indicates the
95      * engine should use the Apache Jakarta Log4J logger package.
96      */

97     public static final int LIBRARY_LOG4J = 3;
98
99     private static final String JavaDoc[] LIBINIT =
100     {
101         "freemarker.log", "Null",
102         "java.util.logging", "JDK14",
103         "org.apache.log", "Avalon",
104         "org.apache.log4j", "Log4J"
105     };
106
107     private static int logLibrary;
108     private static LoggerFactory factory;
109     private static String JavaDoc categoryPrefix = "";
110
111     private static final Map JavaDoc loggers = new HashMap JavaDoc();
112
113     /**
114      * Selects the logger library to use.
115      * If you want to change the default setting, do it early in application
116      * initialization phase, before calling any other FreeMarker API since once
117      * various parts of the FreeMarker library bind to the logging subsystem,
118      * the change in this value will have no effect on them.
119      * @param library one of <tt>LIBRARY_XXX</tt> constants. By default,
120      * {@link #LIBRARY_AUTO} is used.
121      * @throws ClassNotFoundException if an explicit logging library is asked for
122      * (that is, neither NONE, nor AUTO), and it is not found in the classpath.
123      */

124     public static void selectLoggerLibrary(int library)
125     throws
126         ClassNotFoundException JavaDoc
127     {
128         synchronized (Logger.class) {
129             if(library < -1 || (library*2) >= LIBINIT.length)
130             {
131                 throw new IllegalArgumentException JavaDoc();
132             }
133             logLibrary = library;
134             factory = createFactory();
135         }
136     }
137
138     /**
139      * Sets a category prefix. This prefix is prepended to any logger category
140      * name. This makes it possible to have different FreeMarker logger categories
141      * on a per-application basis (better said, per-classloader basis). By default
142      * the category prefix is the empty string. If you set a non-empty category
143      * prefix, be sure to include the trailing separator dot (i.e. "MyApp.")
144      * If you want to change the default setting, do it early in application
145      * initialization phase, before calling any other FreeMarker API since once
146      * various parts of the FreeMarker library bind to the logging subsystem,
147      * the change in this value will have no effect on them.
148      */

149     public static void setCategoryPrefix(String JavaDoc prefix)
150     {
151         synchronized (Logger.class) {
152             if(prefix == null)
153             {
154                 throw new IllegalArgumentException JavaDoc();
155             }
156             categoryPrefix = prefix;
157         }
158     }
159
160     /**
161      * Logs a debugging message.
162      */

163     public abstract void debug(String JavaDoc message);
164
165     /**
166      * Logs a debugging message with accompanying throwable.
167      */

168     public abstract void debug(String JavaDoc message, Throwable JavaDoc t);
169
170     /**
171      * Logs an informational message.
172      */

173     public abstract void info(String JavaDoc message);
174
175     /**
176      * Logs an informational message with accompanying throwable.
177      */

178     public abstract void info(String JavaDoc message, Throwable JavaDoc t);
179
180     /**
181      * Logs a warning message.
182      */

183     public abstract void warn(String JavaDoc message);
184
185     /**
186      * Logs a warning message with accompanying throwable.
187      */

188     public abstract void warn(String JavaDoc message, Throwable JavaDoc t);
189
190     /**
191      * Logs an error message.
192      */

193     public abstract void error(String JavaDoc message);
194
195     /**
196      * Logs an error message with accompanying throwable.
197      */

198     public abstract void error(String JavaDoc message, Throwable JavaDoc t);
199
200     /**
201      * Returns true if this logger will log debug messages.
202      */

203     public abstract boolean isDebugEnabled();
204
205     /**
206      * Returns true if this logger will log informational messages.
207      */

208     public abstract boolean isInfoEnabled();
209
210     /**
211      * Returns true if this logger will log warning messages.
212      */

213     public abstract boolean isWarnEnabled();
214
215     /**
216      * Returns true if this logger will log error messages.
217      */

218     public abstract boolean isErrorEnabled();
219
220     /**
221      * Returns true if this logger will log fatal error messages.
222      */

223     public abstract boolean isFatalEnabled();
224
225     /**
226      * Returns a logger for the specified category.
227      * @param category a dot separated hierarchical category name. If a category
228      * prefix is in effect, it is prepended to the category name.
229      */

230     public static Logger getLogger(String JavaDoc category)
231     {
232         if (factory == null) {
233             synchronized (Logger.class) {
234                 if (factory == null) {
235                     try
236                     {
237                         selectLoggerLibrary(LIBRARY_AUTO);
238                     }
239                     catch(ClassNotFoundException JavaDoc e)
240                     {
241                         // This can't happen, really
242
throw new RuntimeException JavaDoc(e.getMessage());
243                     }
244                 }
245             }
246         }
247
248         category = categoryPrefix + category;
249
250         synchronized(loggers)
251         {
252             Logger logger = (Logger)loggers.get(category);
253             if(logger == null)
254             {
255                 logger = factory.getLogger(category);
256                 loggers.put(category, logger);
257             }
258             return logger;
259         }
260     }
261
262     private static LoggerFactory createFactory()
263     throws
264         ClassNotFoundException JavaDoc
265     {
266         if(logLibrary == LIBRARY_AUTO)
267         {
268             for(int i = LIBINIT.length / 2 - 1; i > 0; --i)
269             {
270                 try
271                 {
272                     return createFactory(i);
273                 }
274                 catch(ClassNotFoundException JavaDoc e)
275                 {
276                     ;//Intentionally ignored
277
}
278             }
279             System.err.println("*** WARNING: FreeMarker logging suppressed.");
280             return new NullLoggerFactory();
281         }
282         else
283         {
284             return createFactory(logLibrary);
285         }
286     }
287
288     private static LoggerFactory createFactory(int library)
289     throws
290         ClassNotFoundException JavaDoc
291     {
292         String JavaDoc requiredPackage = LIBINIT[library * 2];
293         String JavaDoc factoryType = LIBINIT[library * 2 + 1];
294
295         try
296         {
297             ClassUtil.forName(requiredPackage + ".Logger");
298             return (LoggerFactory)Class.forName("freemarker.log." + factoryType + "LoggerFactory").newInstance();
299         }
300         catch(IllegalAccessException JavaDoc e)
301         {
302             // This can't happen, really
303
throw new IllegalAccessError JavaDoc(e.getMessage());
304         }
305         catch(InstantiationException JavaDoc e)
306         {
307             // This can't happen, really
308
throw new InstantiationError JavaDoc(e.getMessage());
309         }
310     }
311 }
312
Popular Tags