001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.common;
010:
011: import org.apache.commons.logging.Log;
012:
013: /**
014: * Logger sending everything to the standard output streams. This is mainly for the cases when you
015: * have a utility that does not have a logger to supply.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
018: * @version CVS $Id: ConsoleLog.java,v 1.4 2003/12/14 09:53:56 benedikta Exp $
019: */
020: public final class ConsoleLog implements Log {
021: public static final int TRACE = 0;
022: public static final int DEBUG = 1;
023: public static final int INFO = 2;
024: public static final int WARN = 3;
025: public static final int ERROR = 4;
026: public static final int FATAL = 5;
027: private int level = DEBUG;
028:
029: public ConsoleLog() {
030: }
031:
032: public ConsoleLog(int level) {
033: this .level = level;
034: }
035:
036: /**
037: * <p>
038: * Log a message with trace log level.
039: * </p>
040: *
041: * @param message log this message
042: */
043: public void trace(Object message) {
044: if (isTraceEnabled()) {
045: System.out.print("[TRACE] ");
046: System.out.println(message);
047: }
048: }
049:
050: /**
051: * <p>
052: * Log an error with trace log level.
053: * </p>
054: *
055: * @param message log this message
056: * @param t log this cause
057: */
058: public void trace(Object message, Throwable t) {
059: if (isTraceEnabled()) {
060: System.out.print("[TRACE] ");
061: System.out.println(message);
062: t.printStackTrace(System.out);
063: }
064: }
065:
066: /**
067: * <p>
068: * Is trace logging currently enabled?
069: * </p>
070: *
071: * <p>
072: * Call this method to prevent having to perform expensive operations (for example,
073: * <code>String</code> concatination) when the log level is more than trace.
074: * </p>
075: */
076: public boolean isTraceEnabled() {
077: return level <= TRACE;
078: }
079:
080: /**
081: * <p>
082: * Log a message with debug log level.
083: * </p>
084: *
085: * @param message log this message
086: */
087: public void debug(Object message) {
088: if (isDebugEnabled()) {
089: System.out.print("[DEBUG] ");
090: System.out.println(message);
091: }
092: }
093:
094: /**
095: * <p>
096: * Log an error with debug log level.
097: * </p>
098: *
099: * @param message log this message
100: * @param t log this cause
101: */
102: public void debug(Object message, Throwable t) {
103: if (isDebugEnabled()) {
104: System.out.print("[DEBUG] ");
105: System.out.println(message);
106: t.printStackTrace(System.out);
107: }
108: }
109:
110: /**
111: * <p>
112: * Is debug logging currently enabled?
113: * </p>
114: *
115: * <p>
116: * Call this method to prevent having to perform expensive operations (for example,
117: * <code>String</code> concatination) when the log level is more than debug.
118: * </p>
119: */
120: public boolean isDebugEnabled() {
121: return level <= DEBUG;
122: }
123:
124: /**
125: * <p>
126: * Log a message with info log level.
127: * </p>
128: *
129: * @param message log this message
130: */
131: public void info(Object message) {
132: if (isInfoEnabled()) {
133: System.out.print("[INFO] ");
134: System.out.println(message);
135: }
136: }
137:
138: /**
139: * <p>
140: * Log an error with info log level.
141: * </p>
142: *
143: * @param message log this message
144: * @param t log this cause
145: */
146: public void info(Object message, Throwable t) {
147: if (isInfoEnabled()) {
148: System.out.print("[INFO] ");
149: System.out.println(message);
150: t.printStackTrace(System.out);
151: }
152: }
153:
154: /**
155: * <p>
156: * Is info logging currently enabled?
157: * </p>
158: *
159: * <p>
160: * Call this method to prevent having to perform expensive operations (for example,
161: * <code>String</code> concatination) when the log level is more than info.
162: * </p>
163: */
164: public boolean isInfoEnabled() {
165: return level <= INFO;
166: }
167:
168: /**
169: * <p>
170: * Log a message with warn log level.
171: * </p>
172: *
173: * @param message log this message
174: */
175: public void warn(Object message) {
176: if (isWarnEnabled()) {
177: System.out.print("[WARN] ");
178: System.out.println(message);
179: }
180: }
181:
182: /**
183: * <p>
184: * Log an error with warn log level.
185: * </p>
186: *
187: * @param message log this message
188: * @param t log this cause
189: */
190: public void warn(Object message, Throwable t) {
191: if (isWarnEnabled()) {
192: System.out.print("[WARN] ");
193: System.out.println(message);
194: t.printStackTrace(System.out);
195: }
196: }
197:
198: /**
199: * <p>
200: * Is warning logging currently enabled?
201: * </p>
202: *
203: * <p>
204: * Call this method to prevent having to perform expensive operations (for example,
205: * <code>String</code> concatination) when the log level is more than warning.
206: * </p>
207: */
208: public boolean isWarnEnabled() {
209: return level <= WARN;
210: }
211:
212: /**
213: * <p>
214: * Log a message with error log level.
215: * </p>
216: *
217: * @param message log this message
218: */
219: public void error(Object message) {
220: if (isErrorEnabled()) {
221: System.out.print("[ERROR] ");
222: System.out.println(message);
223: }
224: }
225:
226: /**
227: * <p>
228: * Log an error with error log level.
229: * </p>
230: *
231: * @param message log this message
232: * @param t log this cause
233: */
234: public void error(Object message, Throwable t) {
235: if (isErrorEnabled()) {
236: System.out.print("[ERROR] ");
237: System.out.println(message);
238: t.printStackTrace(System.out);
239: }
240: }
241:
242: /**
243: * <p>
244: * Is error logging currently enabled?
245: * </p>
246: *
247: * <p>
248: * Call this method to prevent having to perform expensive operations (for example,
249: * <code>String</code> concatination) when the log level is more than error.
250: * </p>
251: */
252: public boolean isErrorEnabled() {
253: return level <= ERROR;
254: }
255:
256: /**
257: * <p>
258: * Log a message with fatal log level.
259: * </p>
260: *
261: * @param message log this message
262: */
263: public void fatal(Object message) {
264: if (isFatalEnabled()) {
265: System.out.print("[FATAL] ");
266: System.out.println(message);
267: }
268: }
269:
270: /**
271: * <p>
272: * Log an error with fatal log level.
273: * </p>
274: *
275: * @param message log this message
276: * @param t log this cause
277: */
278: public void fatal(Object message, Throwable t) {
279: if (isFatalEnabled()) {
280: System.out.print("[FATAL] ");
281: System.out.println(message);
282: t.printStackTrace(System.out);
283: }
284: }
285:
286: /**
287: * <p>
288: * Is fatal logging currently enabled?
289: * </p>
290: *
291: * <p>
292: * Call this method to prevent having to perform expensive operations (for example,
293: * <code>String</code> concatination) when the log level is more than fatal.
294: * </p>
295: */
296: public boolean isFatalEnabled() {
297: return level <= FATAL;
298: }
299: }
|