001: package org.apache.ojb.broker.util.logging;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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:
018: import org.apache.ojb.broker.util.configuration.Configuration;
019: import org.apache.ojb.broker.util.configuration.ConfigurationException;
020:
021: /**
022: * this is a most simple Logger implementation.
023: * All output is directed to System.out.
024: *
025: * @author Thomas Mahler
026: * @version $Id: PoorMansLoggerImpl.java,v 1.11.2.3 2005/12/21 22:28:16 tomdz Exp $
027: */
028: public class PoorMansLoggerImpl implements Logger {
029: protected static final String STR_DEBUG = "DEBUG";
030: protected static final String STR_INFO = "INFO";
031: protected static final String STR_WARN = "WARN";
032: protected static final String STR_ERROR = "ERROR";
033: protected static final String STR_FATAL = "FATAL";
034:
035: protected static final String STR_DEBUG_MSG = "DEBUG: ";
036: protected static final String STR_INFO_MSG = "INFO: ";
037: protected static final String STR_WARN_MSG = "WARN: ";
038: protected static final String STR_ERROR_MSG = "ERROR: ";
039: protected static final String STR_FATAL_MSG = "FATAL: ";
040:
041: protected static final String BRAKE_OPEN = "[";
042: protected static final String BRAKE_CLOSE = "] ";
043:
044: private String name;
045:
046: private int level = 0;
047:
048: public PoorMansLoggerImpl(String name) {
049: this .name = name;
050: }
051:
052: protected int getLevel() {
053: return level;
054: }
055:
056: void setLevel(int pLevel) {
057: level = pLevel;
058: }
059:
060: public String getName() {
061: return name;
062: }
063:
064: /**
065: * generate a message for loglevel DEBUG
066: * @param pObject the message Object
067: */
068: public void debug(Object pObject) {
069: debug(pObject, null);
070: }
071:
072: public void debug(Object message, Throwable t) {
073: if (DEBUG >= getLevel()) {
074: log(STR_DEBUG_MSG, message, t);
075: }
076: }
077:
078: public void safeDebug(String message, Object obj) {
079: safeDebug(message, obj, null);
080: }
081:
082: public void safeDebug(String message, Object obj, Throwable t) {
083: if (DEBUG >= getLevel()) {
084: String toString = null;
085: if (obj != null) {
086: try {
087: toString = obj.toString();
088: } catch (Throwable throwable) {
089: toString = "BAD toString() impl for "
090: + obj.getClass().getName();
091: }
092: }
093: log(STR_DEBUG_MSG, message + " : " + toString, t);
094: }
095: }
096:
097: /**
098: * generate a message for loglevel INFO
099: * @param pObject the message Object
100: */
101: public void info(Object pObject) {
102: info(pObject, null);
103: }
104:
105: public void info(Object message, Throwable t) {
106: if (INFO >= getLevel()) {
107: log(STR_INFO_MSG, message, t);
108: }
109: }
110:
111: public void safeInfo(String message, Object obj) {
112: safeInfo(message, obj, null);
113: }
114:
115: public void safeInfo(String message, Object obj, Throwable t) {
116: if (INFO >= getLevel()) {
117: String toString = null;
118: if (obj != null) {
119: try {
120: toString = obj.toString();
121: } catch (Throwable throwable) {
122: toString = "BAD toString() impl for "
123: + obj.getClass().getName();
124: }
125: }
126: log(STR_INFO_MSG, message + " : " + toString, t);
127: }
128: }
129:
130: /**
131: * generate a message for loglevel WARN
132: * @param pObject the message Object
133: */
134: public void warn(Object pObject) {
135: warn(pObject, null);
136: }
137:
138: public void warn(Object message, Throwable t) {
139: if (WARN >= getLevel()) {
140: log(STR_WARN_MSG, message, t);
141: }
142: }
143:
144: public void safeWarn(String message, Object obj) {
145: safeWarn(message, obj, null);
146: }
147:
148: public void safeWarn(String message, Object obj, Throwable t) {
149: if (WARN >= getLevel()) {
150: String toString = null;
151: if (obj != null) {
152: try {
153: toString = obj.toString();
154: } catch (Throwable throwable) {
155: toString = "BAD toString() impl for "
156: + obj.getClass().getName();
157: }
158: }
159: log(STR_WARN_MSG, message + " : " + toString, t);
160: }
161: }
162:
163: /**
164: * generate a message for loglevel ERROR
165: * @param pObject the message Object
166: */
167: public void error(Object pObject) {
168: error(pObject, null);
169: }
170:
171: public void error(Object message, Throwable t) {
172: if (ERROR >= getLevel()) {
173: log(STR_ERROR_MSG, message, t);
174: }
175: }
176:
177: public void safeError(String message, Object obj) {
178: safeError(message, obj, null);
179: }
180:
181: public void safeError(String message, Object obj, Throwable t) {
182: if (ERROR >= getLevel()) {
183: String toString = null;
184: if (obj != null) {
185: try {
186: toString = obj.toString();
187: } catch (Throwable throwable) {
188: toString = "BAD toString() impl for "
189: + obj.getClass().getName();
190: }
191: }
192: log(STR_ERROR_MSG, message + " : " + toString, t);
193: }
194: }
195:
196: /**
197: * generate a message for loglevel FATAL
198: * @param pObject the message Object
199: */
200: public void fatal(Object pObject) {
201: fatal(pObject, null);
202: }
203:
204: public void fatal(Object message, Throwable t) {
205: if (FATAL >= getLevel()) {
206: log(STR_FATAL_MSG, message, t);
207: }
208: }
209:
210: public void safeFatal(String message, Object obj) {
211: safeFatal(message, obj, null);
212: }
213:
214: public void safeFatal(String message, Object obj, Throwable t) {
215: if (FATAL >= getLevel()) {
216: String toString = null;
217: if (obj != null) {
218: try {
219: toString = obj.toString();
220: } catch (Throwable throwable) {
221: toString = "BAD toString() impl for "
222: + obj.getClass().getName();
223: }
224: }
225: log(STR_FATAL_MSG, message + " : " + toString, t);
226: }
227: }
228:
229: public boolean isDebugEnabled() {
230: return isEnabledFor(DEBUG);
231: }
232:
233: public boolean isEnabledFor(int priority) {
234: return priority >= getLevel();
235: }
236:
237: protected void log(String aLevel, Object obj, Throwable t) {
238: System.out.print(BRAKE_OPEN + name + BRAKE_CLOSE + aLevel);
239: if (obj != null && obj instanceof Throwable) {
240: try {
241: System.out.println(((Throwable) obj).getMessage());
242: ((Throwable) obj).printStackTrace();
243: } catch (Throwable ignored) {
244: /*logging should be failsafe*/
245: }
246: } else {
247: System.out.println(obj);
248: }
249:
250: if (t != null) {
251: try {
252: System.out.println(t.getMessage());
253: t.printStackTrace();
254: } catch (Throwable ignored) {
255: /*logging should be failsafe*/
256: }
257: }
258: }
259:
260: /*
261: * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
262: */
263: public void configure(Configuration config)
264: throws ConfigurationException {
265: LoggingConfiguration lc = (LoggingConfiguration) config;
266: String levelName = lc.getLogLevel(name);
267: setLevel(levelName);
268: }
269:
270: public void setLevel(String levelName) {
271: if (levelName.equalsIgnoreCase(STR_DEBUG)) {
272: level = DEBUG;
273: } else if (levelName.equalsIgnoreCase(STR_INFO)) {
274: level = INFO;
275: } else if (levelName.equalsIgnoreCase(STR_WARN)) {
276: level = WARN;
277: } else if (levelName.equalsIgnoreCase(STR_ERROR)) {
278: level = ERROR;
279: } else if (levelName.equalsIgnoreCase(STR_FATAL)) {
280: level = FATAL;
281: } else {
282: level = WARN;
283: }
284: }
285: }
|