001: /*
002: * Copyright (c) 2004-2005 SLF4J.ORG
003: * Copyright (c) 2004-2005 QOS.ch
004: *
005: * All rights reserved.
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining
008: * a copy of this software and associated documentation files (the
009: * "Software"), to deal in the Software without restriction, including
010: * without limitation the rights to use, copy, modify, merge, publish,
011: * distribute, and/or sell copies of the Software, and to permit persons
012: * to whom the Software is furnished to do so, provided that the above
013: * copyright notice(s) and this permission notice appear in all copies of
014: * the Software and that both the above copyright notice(s) and this
015: * permission notice appear in supporting documentation.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
018: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
020: * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
021: * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
022: * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
023: * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
024: * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
025: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
026: *
027: * Except as contained in this notice, the name of a copyright holder
028: * shall not be used in advertising or otherwise to promote the sale, use
029: * or other dealings in this Software without prior written authorization
030: * of the copyright holder.
031: *
032: */
033:
034: package org.slf4j.impl;
035:
036: import org.slf4j.helpers.MarkerIgnoringBase;
037: import org.slf4j.helpers.MessageFormatter;
038:
039: /**
040: * A simple (and direct) implementation that logs messages of level
041: * INFO or higher on the console (<code>System.err<code>).
042: *
043: * <p>The output includes the relative time in milliseconds, thread
044: * name, the level, logger name, and the message followed by the line
045: * separator for the host. In log4j terms it amounts to the "%r [%t]
046: * %level %logger - %m%n" pattern. </p>
047: *
048: * <p>Sample output follows.</p>
049: <pre>
050: 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
051: 225 [main] INFO examples.SortAlgo - Entered the sort method.
052: 304 [main] INFO examples.SortAlgo - Dump of integer array:
053: 317 [main] INFO examples.SortAlgo - Element [0] = 0
054: 331 [main] INFO examples.SortAlgo - Element [1] = 1
055: 343 [main] INFO examples.Sort - The next log statement should be an error message.
056: 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array.
057: at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
058: at org.log4j.examples.Sort.main(Sort.java:64)
059: 467 [main] INFO examples.Sort - Exiting main method.
060: </pre>
061: *
062: * @author Ceki Gülcü
063: */
064: public class SimpleLogger extends MarkerIgnoringBase {
065: /**
066: * Mark the time when this class gets loaded into memory.
067: */
068: private static long startTime = System.currentTimeMillis();
069: public static final String LINE_SEPARATOR = System
070: .getProperty("line.separator");
071: private static String INFO_STR = "INFO";
072: private static String WARN_STR = "WARN";
073: private static String ERROR_STR = "ERROR";
074: String name;
075:
076: /**
077: * Package access allows only {@link SimpleLoggerFactory} to instantiate
078: * SimpleLogger instances.
079: */
080: SimpleLogger(String name) {
081: this .name = name;
082: }
083:
084: public String getName() {
085: return name;
086: }
087:
088: /**
089: * Always returns false.
090: * @return always false
091: */
092: public boolean isTraceEnabled() {
093: return false;
094: }
095:
096: /**
097: * A NOP implementation, as this logger is permanently disabled for
098: * the TRACE level.
099: */
100: public void trace(String msg) {
101: // NOP
102: }
103:
104: /**
105: * A NOP implementation, as this logger is permanently disabled for
106: * the TRACE level.
107: */
108: public void trace(String format, Object param1) {
109: // NOP
110: }
111:
112: /**
113: * A NOP implementation, as this logger is permanently disabled for
114: * the TRACE level.
115: */
116: public void trace(String format, Object param1, Object param2) {
117: // NOP
118: }
119:
120: public void trace(String format, Object[] argArray) {
121: // NOP
122: }
123:
124: /**
125: * A NOP implementation, as this logger is permanently disabled for
126: * the TRACE level.
127: */
128: public void trace(String msg, Throwable t) {
129: // NOP
130: }
131:
132: /**
133: * Always returns false.
134: * @return always false
135: */
136: public boolean isDebugEnabled() {
137: return false;
138: }
139:
140: /**
141: * A NOP implementation, as this logger is permanently disabled for
142: * the DEBUG level.
143: */
144: public void debug(String msg) {
145: // NOP
146: }
147:
148: /**
149: * A NOP implementation, as this logger is permanently disabled for
150: * the DEBUG level.
151: */
152: public void debug(String format, Object param1) {
153: // NOP
154: }
155:
156: /**
157: * A NOP implementation, as this logger is permanently disabled for
158: * the DEBUG level.
159: */
160: public void debug(String format, Object param1, Object param2) {
161: // NOP
162: }
163:
164: public void debug(String format, Object[] argArray) {
165: // NOP
166: }
167:
168: /**
169: * A NOP implementation, as this logger is permanently disabled for
170: * the DEBUG level.
171: */
172: public void debug(String msg, Throwable t) {
173: // NOP
174: }
175:
176: /**
177: * This is our internal implementation for logging regular (non-parameterized)
178: * log messages.
179: *
180: * @param level
181: * @param message
182: * @param t
183: */
184: private void log(String level, String message, Throwable t) {
185: StringBuffer buf = new StringBuffer();
186:
187: long millis = System.currentTimeMillis();
188: buf.append(millis - startTime);
189:
190: buf.append(" [");
191: buf.append(Thread.currentThread().getName());
192: buf.append("] ");
193:
194: buf.append(level);
195: buf.append(" ");
196:
197: buf.append(name);
198: buf.append(" - ");
199:
200: buf.append(message);
201:
202: buf.append(LINE_SEPARATOR);
203:
204: System.err.print(buf.toString());
205: if (t != null) {
206: t.printStackTrace(System.err);
207: }
208: System.err.flush();
209: }
210:
211: /**
212: * For formatted messages, first substitute arguments and then log.
213: *
214: * @param level
215: * @param format
216: * @param param1
217: * @param param2
218: */
219: private void formatAndLog(String level, String format, Object arg1,
220: Object arg2) {
221: String message = MessageFormatter.format(format, arg1, arg2);
222: log(level, message, null);
223: }
224:
225: /**
226: * For formatted messages, first substitute arguments and then log.
227: *
228: * @param level
229: * @param format
230: * @param argArray
231: */
232: private void formatAndLog(String level, String format,
233: Object[] argArray) {
234: String message = MessageFormatter.arrayFormat(format, argArray);
235: log(level, message, null);
236: }
237:
238: /**
239: * Always returns true.
240: */
241: public boolean isInfoEnabled() {
242: return true;
243: }
244:
245: /**
246: * A simple implementation which always logs messages of level INFO according
247: * to the format outlined above.
248: */
249: public void info(String msg) {
250: log(INFO_STR, msg, null);
251: }
252:
253: /**
254: * Perform single parameter substitution before logging the message of level
255: * INFO according to the format outlined above.
256: */
257: public void info(String format, Object arg) {
258: formatAndLog(INFO_STR, format, arg, null);
259: }
260:
261: /**
262: * Perform double parameter substitution before logging the message of level
263: * INFO according to the format outlined above.
264: */
265: public void info(String format, Object arg1, Object arg2) {
266: formatAndLog(INFO_STR, format, arg1, arg2);
267: }
268:
269: /**
270: * Perform double parameter substitution before logging the message of level
271: * INFO according to the format outlined above.
272: */
273: public void info(String format, Object[] argArray) {
274: formatAndLog(INFO_STR, format, argArray);
275: }
276:
277: /**
278: * Log a message of level INFO, including an exception.
279: */
280: public void info(String msg, Throwable t) {
281: log(INFO_STR, msg, t);
282: }
283:
284: /**
285: * Always returns true.
286: */
287: public boolean isWarnEnabled() {
288: return true;
289: }
290:
291: /**
292: * A simple implementation which always logs messages of level WARN according
293: * to the format outlined above.
294: */
295: public void warn(String msg) {
296: log(WARN_STR, msg, null);
297: }
298:
299: /**
300: * Perform single parameter substitution before logging the message of level
301: * WARN according to the format outlined above.
302: */
303: public void warn(String format, Object arg) {
304: formatAndLog(WARN_STR, format, arg, null);
305: }
306:
307: /**
308: * Perform double parameter substitution before logging the message of level
309: * WARN according to the format outlined above.
310: */
311: public void warn(String format, Object arg1, Object arg2) {
312: formatAndLog(WARN_STR, format, arg1, arg2);
313: }
314:
315: /**
316: * Perform double parameter substitution before logging the message of level
317: * WARN according to the format outlined above.
318: */
319: public void warn(String format, Object[] argArray) {
320: formatAndLog(WARN_STR, format, argArray);
321: }
322:
323: /**
324: * Log a message of level WARN, including an exception.
325: */
326: public void warn(String msg, Throwable t) {
327: log(WARN_STR, msg, t);
328: }
329:
330: /**
331: * Always returns true.
332: */
333: public boolean isErrorEnabled() {
334: return true;
335: }
336:
337: /**
338: * A simple implementation which always logs messages of level ERROR according
339: * to the format outlined above.
340: */
341: public void error(String msg) {
342: log(ERROR_STR, msg, null);
343: }
344:
345: /**
346: * Perform single parameter substitution before logging the message of level
347: * ERROR according to the format outlined above.
348: */
349: public void error(String format, Object arg) {
350: formatAndLog(ERROR_STR, format, arg, null);
351: }
352:
353: /**
354: * Perform double parameter substitution before logging the message of level
355: * ERROR according to the format outlined above.
356: */
357: public void error(String format, Object arg1, Object arg2) {
358: formatAndLog(ERROR_STR, format, arg1, arg2);
359: }
360:
361: /**
362: * Perform double parameter substitution before logging the message of level
363: * ERROR according to the format outlined above.
364: */
365: public void error(String format, Object[] argArray) {
366: formatAndLog(ERROR_STR, format, argArray);
367: }
368:
369: /**
370: * Log a message of level ERROR, including an exception.
371: */
372: public void error(String msg, Throwable t) {
373: log(ERROR_STR, msg, t);
374: }
375: }
|