Source code: ojb/broker/util/logging/Log4jLoggerImpl.java
1 /*
2 * ObJectRelationalBridge - Bridging Java Objects and Relational Databases
3 * http://objectbridge.sourceforge.net
4 * Copyright (C) 2000, 2001 Thomas Mahler, et al.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 package ojb.broker.util.logging;
21
22 import java.net.URL;
23 import java.util.HashMap;
24
25 import ojb.broker.util.configuration.Configuration;
26 import ojb.broker.util.configuration.ConfigurationException;
27 import org.apache.log4j.Category;
28 import org.apache.log4j.Priority;
29 import org.apache.log4j.PropertyConfigurator;
30
31 /**
32 * This is a Logger implementation based on Log4j.
33 * It can be enabled by putting
34 * LoggerClass=ojb.broker.util.logging.Log4jLoggerImpl
35 * in the OJB .properties file. <br>
36 * If you want log4j to initialize from a property file you can add
37 * LoggerConfigFile=log4j.properties to the ojb.properties file.
38 * the logger only initializes log4j if the application hasn't done it yet
39 *
40 * You can find sample log4j.properties file in the log4j web site
41 * http://jakarta.apache.org/log4j
42 * in the javadoc look for org.apache.log4j.examples
43 *
44 * @author Bertrand
45 * @author Thomas Mahler
46 * @version $Id: Log4jLoggerImpl.java,v 1.4 2002/05/06 23:19:02 cgreenlee Exp $
47 */
48 public class Log4jLoggerImpl implements Logger
49 {
50 static private HashMap priorityMap;
51 static private boolean log4jconfigured = false;
52 private transient Category cat;
53 private String name;
54
55 private String configFile = null;
56
57 static
58 {
59 priorityMap = new HashMap();
60 priorityMap.put(new Integer(Logger.DEBUG), Priority.DEBUG);
61 priorityMap.put(new Integer(Logger.INFO), Priority.INFO);
62 priorityMap.put(new Integer(Logger.WARN), Priority.WARN);
63 priorityMap.put(new Integer(Logger.ERROR), Priority.ERROR);
64 priorityMap.put(new Integer(Logger.FATAL), Priority.FATAL);
65 }
66
67 public Log4jLoggerImpl(String name)
68 {
69 this.name = name;
70 if (!log4jconfigured)
71 {
72 setConfigurationFile(null);
73 log4jconfigured = true;
74 }
75 cat = Category.getInstance(name);
76 }
77
78 /**
79 * Gets the cat.
80 * @return Returns a Category
81 */
82 private Category getCat()
83 {
84 if (cat == null)
85 {
86 cat = Category.getInstance(name);
87 }
88 return cat;
89 }
90
91 /**
92 * generate a message for loglevel DEBUG
93 * @param pObject the message Object
94 */
95 public final void debug(Object pObject)
96 {
97 getCat().debug(pObject);
98 }
99
100 /**
101 * generate a message for loglevel INFO
102 * @param pObject the message Object
103 */
104 public final void info(Object pObject)
105 {
106 getCat().info(pObject);
107 }
108
109 /**
110 * generate a message for loglevel WARN
111 * @param pObject the message Object
112 */
113 public final void warn(Object pObject)
114 {
115 getCat().warn(pObject);
116 }
117
118 /**
119 * generate a message for loglevel ERROR
120 * @param pObject the message Object
121 */
122 public final void error(Object pObject)
123 {
124 getCat().error(pObject);
125 }
126
127 /**
128 * generate a message for loglevel FATAL
129 * @param pObject the message Object
130 */
131 public final void fatal(Object pObject)
132 {
133 getCat().fatal(pObject);
134 }
135
136 public void debug(Object message, Throwable obj)
137 {
138 getCat().debug(message, obj);
139 }
140
141 public void error(Object message, Throwable obj)
142 {
143 getCat().error(message, obj);
144 }
145
146 public void fatal(Object message, Throwable obj)
147 {
148 getCat().fatal(message, obj);
149 }
150
151 public void info(Object message, Throwable obj)
152 {
153 getCat().info(message, obj);
154 }
155
156 public void warn(Object message, Throwable obj)
157 {
158 getCat().warn(message, obj);
159 }
160
161 public boolean isDebugEnabled()
162 {
163 return getCat().isDebugEnabled();
164 }
165
166 public boolean isEnabledFor(int priority)
167 {
168 return getCat().isEnabledFor((Priority) priorityMap.get(
169 new Integer(priority)));
170 }
171
172 /**
173 * @see Configurable#configure(Configuration)
174 */
175 public void configure(Configuration config) throws ConfigurationException
176 {
177 LoggingConfiguration lc = (LoggingConfiguration) config;
178 setConfigurationFile(lc.getLoggerConfigFile());
179 }
180
181 private void setConfigurationFile(String configurationFile) {
182
183 // Only configure the PropertyConfigurator if we've got a new configFile.
184 if (this.configFile == null || !this.configFile.equals(configurationFile))
185 {
186 this.configFile = configurationFile;
187
188 if (this.configFile != null)
189 {
190 // try resource look in classpath
191 URL url = Thread.currentThread().getContextClassLoader().getResource(this.configFile);
192 LoggerFactory.getBootLogger().debug(
193 "Initializing Log4J logger, configFile url["
194 + url + "]");
195
196 if (url != null)
197 {
198 PropertyConfigurator.configure(url.getFile());
199 }
200 // if file is not in classpath try ordinary filesystem lookup
201 else if (this.configFile != "")
202 {
203 PropertyConfigurator.configure(configFile);
204 }
205 else
206 {
207 PropertyConfigurator.configure();
208 }
209 }
210 else
211 {
212 PropertyConfigurator.configure();
213 }
214 }
215 }
216
217 }