001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import org.codehaus.dna.Logger;
011:
012: import java.io.PrintStream;
013:
014: /**
015: * A simple logger facade that simply writes to the Console.
016: *
017: * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
018: */
019: public class ConsoleLogger implements Logger {
020: /**
021: * Constant to indicate that the logger
022: * must log all levels.
023: */
024: public static final int LEVEL_ALL = 0;
025:
026: /**
027: * Constant to indicate that the logger
028: * must log all levels TRACE and above.
029: */
030: public static final int LEVEL_TRACE = 1;
031:
032: /**
033: * Constant to indicate that the logger
034: * must log all levels DEBUG and above.
035: */
036: public static final int LEVEL_DEBUG = 2;
037:
038: /**
039: * Constant to indicate that the logger
040: * must log all levels INFO and above.
041: */
042: public static final int LEVEL_INFO = 3;
043:
044: /**
045: * Constant to indicate that the logger
046: * must log all levels WARN and above.
047: */
048: public static final int LEVEL_WARN = 4;
049:
050: /**
051: * Constant to indicate that the logger
052: * must log all levels ERROR and above.
053: */
054: public static final int LEVEL_ERROR = 5;
055:
056: /**
057: * Constant to indicate that the logger
058: * must not log any messages.
059: */
060: public static final int LEVEL_NONE = 6;
061:
062: /**
063: * String constant used to output TRACE messages.
064: */
065: private static final String LEVEL_TRACE_STR = "TRACE";
066:
067: /**
068: * String constant used to output DEBUG messages.
069: */
070: private static final String LEVEL_DEBUG_STR = "DEBUG";
071:
072: /**
073: * String constant used to output INFO messages.
074: */
075: private static final String LEVEL_INFO_STR = "INFO";
076:
077: /**
078: * String constant used to output WARN messages.
079: */
080: private static final String LEVEL_WARN_STR = "WARN";
081:
082: /**
083: * String constant used to output ERROR messages.
084: */
085: private static final String LEVEL_ERROR_STR = "ERROR";
086:
087: /**
088: * The log level.
089: */
090: private final int m_level;
091:
092: /**
093: * The output location.
094: */
095: private final PrintStream m_output;
096:
097: /**
098: * Create a Console Logger that logs all messages.
099: */
100: public ConsoleLogger() {
101: this (LEVEL_ALL);
102: }
103:
104: /**
105: * Create a Console Logger that logs at specified level.
106: *
107: * @param level one of the LEVEL_* constants
108: */
109: public ConsoleLogger(final int level) {
110: this (level, System.out);
111: }
112:
113: /**
114: * Create a Console Logger that logs at specified level.
115: *
116: * @param level one of the LEVEL_* constants
117: * @param output the stream to output to
118: */
119: public ConsoleLogger(final int level, final PrintStream output) {
120: if (null == output) {
121: throw new NullPointerException("output");
122: }
123: m_level = level;
124: m_output = output;
125: }
126:
127: /**
128: * Log a trace message.
129: *
130: * @param message the message
131: */
132: public void trace(final String message) {
133: trace(message, null);
134: }
135:
136: /**
137: * Log a trace message with an associated throwable.
138: *
139: * @param message the message
140: * @param throwable the throwable
141: */
142: public void trace(final String message, final Throwable throwable) {
143: output(LEVEL_TRACE, LEVEL_TRACE_STR, message, throwable);
144: }
145:
146: /**
147: * Return true if a trace message will be logged.
148: *
149: * @return true if message will be logged
150: */
151: public boolean isTraceEnabled() {
152: return m_level <= LEVEL_TRACE;
153: }
154:
155: /**
156: * Log a debug message.
157: *
158: * @param message the message
159: */
160: public void debug(final String message) {
161: debug(message, null);
162: }
163:
164: /**
165: * Log a debug message with an associated throwable.
166: *
167: * @param message the message
168: * @param throwable the throwable
169: */
170: public void debug(final String message, final Throwable throwable) {
171: output(LEVEL_DEBUG, LEVEL_DEBUG_STR, message, throwable);
172: }
173:
174: /**
175: * Return true if a debug message will be logged.
176: *
177: * @return true if message will be logged
178: */
179: public boolean isDebugEnabled() {
180: return m_level <= LEVEL_DEBUG;
181: }
182:
183: /**
184: * Log a info message.
185: *
186: * @param message the message
187: */
188: public void info(final String message) {
189: info(message, null);
190: }
191:
192: /**
193: * Log a info message with an associated throwable.
194: *
195: * @param message the message
196: * @param throwable the throwable
197: */
198: public void info(final String message, final Throwable throwable) {
199: output(LEVEL_INFO, LEVEL_INFO_STR, message, throwable);
200: }
201:
202: /**
203: * Return true if an info message will be logged.
204: *
205: * @return true if message will be logged
206: */
207: public boolean isInfoEnabled() {
208: return m_level <= LEVEL_INFO;
209: }
210:
211: /**
212: * Log a warn message.
213: *
214: * @param message the message
215: */
216: public void warn(final String message) {
217: warn(message, null);
218: }
219:
220: /**
221: * Log a warn message with an associated throwable.
222: *
223: * @param message the message
224: * @param throwable the throwable
225: */
226: public void warn(final String message, final Throwable throwable) {
227: output(LEVEL_WARN, LEVEL_WARN_STR, message, throwable);
228: }
229:
230: /**
231: * Return true if a warn message will be logged.
232: *
233: * @return true if message will be logged
234: */
235: public boolean isWarnEnabled() {
236: return m_level <= LEVEL_WARN;
237: }
238:
239: /**
240: * Log a error message.
241: *
242: * @param message the message
243: */
244: public void error(final String message) {
245: error(message, null);
246: }
247:
248: /**
249: * Log a error message with an associated throwable.
250: *
251: * @param message the message
252: * @param throwable the throwable
253: */
254: public void error(final String message, final Throwable throwable) {
255: output(LEVEL_ERROR, LEVEL_ERROR_STR, message, throwable);
256: }
257:
258: /**
259: * Return true if a error message will be logged.
260: *
261: * @return true if message will be logged
262: */
263: public boolean isErrorEnabled() {
264: return m_level <= LEVEL_ERROR;
265: }
266:
267: /**
268: * Get the child logger with specified name.
269: *
270: * @param name the name of child logger
271: * @return the child logger
272: */
273: public Logger getChildLogger(final String name) {
274: return this ;
275: }
276:
277: /**
278: * Utility method that logs output if level
279: * is enabled.
280: *
281: * @param level the log level
282: * @param type the type string
283: * @param message the message
284: * @param throwable the throwable, may be null
285: */
286: private void output(final int level, final String type,
287: final String message, final Throwable throwable) {
288: if (m_level <= level) {
289: doOutput(type, message, throwable);
290: }
291: }
292:
293: /**
294: * Utility method to actually output message to console.
295: *
296: * @param type the type string
297: * @param message the message
298: * @param throwable the throwable, may be null
299: */
300: void doOutput(final String type, final String message,
301: final Throwable throwable) {
302: synchronized (System.out) {
303: m_output.println("[" + type + "] " + message);
304: if (null != throwable) {
305: throwable.printStackTrace(m_output);
306: }
307: }
308: }
309:
310: /**
311: * Utility method so that subclasses can access log level.
312: *
313: * @return log level of logger
314: */
315: final int getLevel() {
316: return m_level;
317: }
318: }
|