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: package org.apache.cocoon.ojb.components;
018:
019: import org.apache.avalon.framework.activity.Initializable;
020: import org.apache.avalon.framework.component.Component;
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023:
024: import org.apache.ojb.broker.util.configuration.Configuration;
025: import org.apache.ojb.broker.util.configuration.ConfigurationException;
026: import org.apache.ojb.broker.util.logging.Logger;
027: import org.apache.ojb.broker.util.logging.LoggingConfiguration;
028:
029: /**
030: * OJB logger implementation delegating to the Avalon logger.
031: *
032: * <p>This class has two faces to it:
033: * <dl>
034: * <dt>Avalon Component</dt>
035: * <dd>Instance of the class created and managed by Avalon container.
036: * When instance is initialized, it obtains logger instance to be used
037: * by OJB.</dd>
038: * <dt>OJB Managed Class</dt>
039: * <dd>Instances of the class are created and managed by OJB, as defined
040: * in the OJB <code>OJB.properties</code> file. Each OJB managed instance
041: * of the class will have access to the logger object initialized
042: * by Avalon managed instance of the class.</dd>
043: * </dl>
044: *
045: * It is important that Avalon component is initialized before any access
046: * to OJB API is made.</p>
047: *
048: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
049: * @version $Id: LoggerImpl.java 433543 2006-08-22 06:22:54Z crossley $
050: */
051: public class LoggerImpl extends AbstractLogEnabled implements
052: Component, ThreadSafe, Initializable, Logger {
053:
054: /**
055: * Root logger for all OJB loggers
056: */
057: private static org.apache.avalon.framework.logger.Logger LOGGER;
058:
059: private final String name;
060: private transient int level;
061: private transient org.apache.avalon.framework.logger.Logger logger;
062:
063: /**
064: * Constructor used by Container
065: */
066: public LoggerImpl() {
067: this .name = null;
068: }
069:
070: /**
071: * Constructor used by OJB 1.0 to create a logger instance
072: */
073: public LoggerImpl(String name) {
074: this .name = name.startsWith("org.apache.ojb.") ? name
075: .substring(15) : name;
076: }
077:
078: /**
079: * Constructor used by OJB 1.1 to create a logger instance
080: */
081: public LoggerImpl(String name, LoggingConfiguration config) {
082: this (name);
083: }
084:
085: /**
086: * Set root logger instance which will be used by OJB
087: */
088: public void initialize() {
089: LOGGER = getLogger();
090: }
091:
092: protected int getLevel() {
093: if (logger == null) {
094: this .logger = LOGGER.getChildLogger(this .name);
095: if (this .logger.isDebugEnabled())
096: this .level = DEBUG;
097: else if (this .logger.isInfoEnabled())
098: this .level = INFO;
099: else if (this .logger.isWarnEnabled())
100: this .level = WARN;
101: else if (this .logger.isErrorEnabled())
102: this .level = ERROR;
103: else
104: this .level = FATAL;
105: }
106: return level;
107: }
108:
109: public String getName() {
110: return name;
111: }
112:
113: public void debug(Object message) {
114: if (DEBUG >= getLevel()) {
115: logger.debug(toString(message));
116: }
117: }
118:
119: public void debug(Object message, Throwable t) {
120: if (DEBUG >= getLevel()) {
121: logger.debug(toString(message), t);
122: }
123: }
124:
125: public void safeDebug(String message, Object obj) {
126: if (DEBUG >= getLevel()) {
127: logger.debug(message + " : " + toString(obj));
128: }
129: }
130:
131: public void safeDebug(String message, Object obj, Throwable t) {
132: if (DEBUG >= getLevel()) {
133: logger.debug(message + " : " + toString(obj), t);
134: }
135: }
136:
137: public void info(Object message) {
138: if (INFO >= getLevel()) {
139: logger.info(toString(message));
140: }
141: }
142:
143: public void info(Object message, Throwable t) {
144: if (INFO >= getLevel()) {
145: logger.info(toString(message), t);
146: }
147: }
148:
149: public void safeInfo(String message, Object obj) {
150: if (INFO >= getLevel()) {
151: logger.info(message + " : " + toString(obj));
152: }
153: }
154:
155: public void safeInfo(String message, Object obj, Throwable t) {
156: if (INFO >= getLevel()) {
157: logger.info(message + " : " + toString(obj), t);
158: }
159: }
160:
161: public void warn(Object message) {
162: if (WARN >= getLevel()) {
163: logger.warn(toString(message));
164: }
165: }
166:
167: public void warn(Object message, Throwable t) {
168: if (WARN >= getLevel()) {
169: logger.warn(toString(message), t);
170: }
171: }
172:
173: public void safeWarn(String message, Object obj) {
174: if (WARN >= getLevel()) {
175: logger.warn(message + " : " + toString(obj));
176: }
177: }
178:
179: public void safeWarn(String message, Object obj, Throwable t) {
180: if (WARN >= getLevel()) {
181: logger.warn(message + " : " + toString(obj), t);
182: }
183: }
184:
185: public void error(Object message) {
186: if (ERROR >= getLevel()) {
187: logger.error(toString(message));
188: }
189: }
190:
191: public void error(Object message, Throwable t) {
192: if (ERROR >= getLevel()) {
193: logger.error(toString(message), t);
194: }
195: }
196:
197: public void safeError(String message, Object obj) {
198: if (ERROR >= getLevel()) {
199: logger.error(message + " : " + toString(obj));
200: }
201: }
202:
203: public void safeError(String message, Object obj, Throwable t) {
204: if (ERROR >= getLevel()) {
205: logger.error(message + " : " + toString(obj), t);
206: }
207: }
208:
209: public void fatal(Object message) {
210: if (FATAL >= getLevel()) {
211: logger.fatalError(toString(message));
212: }
213: }
214:
215: public void fatal(Object message, Throwable t) {
216: if (FATAL >= getLevel()) {
217: logger.fatalError(toString(message), t);
218: }
219: }
220:
221: public void safeFatal(String message, Object obj) {
222: if (FATAL >= getLevel()) {
223: logger.fatalError(message + " : " + toString(obj));
224: }
225: }
226:
227: public void safeFatal(String message, Object obj, Throwable t) {
228: if (FATAL >= getLevel()) {
229: logger.fatalError(message + " : " + toString(obj), t);
230: }
231: }
232:
233: public boolean isDebugEnabled() {
234: return isEnabledFor(DEBUG);
235: }
236:
237: public boolean isEnabledFor(int priority) {
238: return priority >= getLevel();
239: }
240:
241: /*
242: * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
243: */
244: public void configure(Configuration config)
245: throws ConfigurationException {
246: }
247:
248: private String toString(Object obj) {
249: if (obj != null) {
250: try {
251: return obj.toString();
252: } catch (Throwable throwable) {
253: return "BAD toString() impl for "
254: + obj.getClass().getName();
255: }
256: }
257:
258: return "null";
259: }
260: }
|