KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > common > Logger


1 /*********************************************************************
2 *
3 * Copyright (C) 2003 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package common;
21
22 import java.security.AccessControlException JavaDoc;
23
24 /**
25  * Abstract wrapper class for the logging interface of choice.
26  * The methods declared here are the same as those for the log4j
27  */

28 public abstract class Logger
29 {
30   /**
31    * The singleton logger
32    */

33   private static Logger logger = null;
34
35   /**
36    * Factory method to return the logger
37    */

38   public static final Logger getLogger(Class JavaDoc cl)
39   {
40     if (logger == null)
41     {
42       initializeLogger();
43     }
44
45     return logger.getLoggerImpl(cl);
46   }
47
48   /**
49    * Initializes the logger in a thread safe manner
50    */

51   private synchronized static void initializeLogger()
52   {
53     if (logger != null)
54     {
55       return;
56     }
57
58     String JavaDoc loggerName = common.log.LoggerName.NAME;
59
60     try
61     {
62       // First see if there was anything defined at run time
63
loggerName = System.getProperty("logger");
64
65       if (loggerName == null)
66       {
67         // Get the logger name from the compiled in logger
68
loggerName = common.log.LoggerName.NAME;
69       }
70
71       logger = (Logger) Class.forName(loggerName).newInstance();
72     }
73     catch(IllegalAccessException JavaDoc e)
74     {
75       logger = new common.log.SimpleLogger();
76       logger.warn("Could not instantiate logger " + loggerName +
77                   " using default");
78     }
79     catch(InstantiationException JavaDoc e)
80     {
81       logger = new common.log.SimpleLogger();
82       logger.warn("Could not instantiate logger " + loggerName +
83                   " using default");
84     }
85     catch (AccessControlException JavaDoc e)
86     {
87       logger = new common.log.SimpleLogger();
88       logger.warn("Could not instantiate logger " + loggerName +
89                   " using default");
90     }
91     catch(ClassNotFoundException JavaDoc e)
92     {
93       logger = new common.log.SimpleLogger();
94       logger.warn("Could not instantiate logger " + loggerName +
95                   " using default");
96     }
97   }
98
99   /**
100    * Constructor
101    */

102   protected Logger()
103   {
104   }
105
106   /**
107    * Log a debug message
108    */

109   public abstract void debug(Object JavaDoc message);
110
111   /**
112    * Log a debug message and exception
113    */

114   public abstract void debug(Object JavaDoc message, Throwable JavaDoc t);
115
116   /**
117    * Log an error message
118    */

119   public abstract void error(Object JavaDoc message);
120
121   /**
122    * Log an error message object and exception
123    */

124   public abstract void error(Object JavaDoc message, Throwable JavaDoc t);
125
126   /**
127    * Log a fatal message
128    */

129   public abstract void fatal(Object JavaDoc message);
130
131   /**
132    * Log a fatal message and exception
133    */

134   public abstract void fatal(Object JavaDoc message, Throwable JavaDoc t);
135
136   /**
137    * Log an information message
138    */

139   public abstract void info(Object JavaDoc message);
140
141   /**
142    * Logs an information message and an exception
143    */

144   public abstract void info(Object JavaDoc message, Throwable JavaDoc t);
145
146   /**
147    * Log a warning message object
148    */

149   public abstract void warn(Object JavaDoc message);
150
151   /**
152    * Log a warning message with exception
153    */

154   public abstract void warn(Object JavaDoc message, Throwable JavaDoc t);
155
156   /**
157    * Accessor to the logger implementation
158    */

159   protected abstract Logger getLoggerImpl(Class JavaDoc cl);
160
161   /**
162    * Empty implementation of the suppressWarnings. Subclasses may
163    * or may not override this method. This method is included
164    * primarily for backwards support of the jxl.nowarnings property, and
165    * is used only by the SimpleLogger
166    *
167    * @param w suppression flag
168    */

169   public void setSuppressWarnings(boolean w)
170   {
171     // default implementation does nothing
172
}
173 }
174
Popular Tags