View Javadoc
1   /**
2    * Copyright (c) 2004-2012 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free of charge, to any person obtaining
6    * a copy of this software and associated documentation files (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.impl;
26  
27  import java.io.PrintStream;
28  import java.util.Date;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.event.LoggingEvent;
32  import org.slf4j.helpers.FormattingTuple;
33  import org.slf4j.helpers.MarkerIgnoringBase;
34  import org.slf4j.helpers.MessageFormatter;
35  import org.slf4j.spi.LocationAwareLogger;
36  
37  /**
38   * <p>
39   * Simple implementation of {@link Logger} that sends all enabled log messages,
40   * for all defined loggers, to the console ({@code System.err}). The following
41   * system properties are supported to configure the behavior of this logger:
42   * </p>
43   *
44   * <ul>
45   * <li><code>org.slf4j.simpleLogger.logFile</code> - The output target which can
46   * be the <em>path</em> to a file, or the special values "System.out" and
47   * "System.err". Default is "System.err".</li>
48   * 
49   * <li><code>org.slf4j.simpleLogger.cacheOutputStream</code> - If the output
50   * target is set to "System.out" or "System.err" (see preceding entry), by
51   * default, logs will be output to the latest value referenced by
52   * <code>System.out/err</code> variables. By setting this
53   * parameter to true, the output stream will be cached, i.e. assigned once at
54   * initialization time and re-used independently of the current value referenced by
55   *  <code>System.out/err</code>.
56   * </li>
57   * 
58   * <li><code>org.slf4j.simpleLogger.defaultLogLevel</code> - Default log level
59   * for all instances of SimpleLogger. Must be one of "trace", "debug", "info",
60   * "warn", "error" or "off". If not specified, defaults to "info".</li>
61   *
62   * <li><code>org.slf4j.simpleLogger.log.<em>com.foo.bar</em></code> - Logging detail
63   * level for a SimpleLogger instance named "com.foo.bar". Right-side value must be one
64   * of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger
65   * named "a.b.c" is initialized, its level is assigned from this property. If
66   * unspecified, the level of nearest parent logger will be used, and if none is
67   * set, then the value specified by
68   * <code>org.slf4j.simpleLogger.defaultLogLevel</code> will be used.</li>
69   *
70   * <li><code>org.slf4j.simpleLogger.showDateTime</code> - If you would like the 
71   * current date and time to be included in output messages, then set it to
72   * <code>true</code>. Default is <code>false</code></li>
73   *
74   * <li><code>org.slf4j.simpleLogger.dateTimeFormat</code> - The date and time
75   * format to be used in the output messages. The pattern describing the date and
76   * time format is defined by <a href=
77   * "http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html">
78   * <code>SimpleDateFormat</code></a>. If the format is not specified or is
79   * invalid, the number of milliseconds since start up will be output.</li>
80   *
81   * <li><code>org.slf4j.simpleLogger.showThreadName</code> - If you want to output 
82   * the current thread name, then set it to <code>true</code>. Defaults to
83   * <code>true</code>.</li>
84   *
85   * <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> - 
86   * If you would like to output the current thread id, then set to
87   * <code>true</code>. Defaults to <code>false</code>.</li>
88   * 
89   * <li><code>org.slf4j.simpleLogger.showLogName</code> - If you would like 
90   * the Logger instance name to be included in output messages, then set it to
91   * <code>true</code>. Defaults to <code>true</code>.</li>
92   *
93   * <li><code>org.slf4j.simpleLogger.showShortLogName</code> - Set to
94   * <code>true</code> if you want the last component of the name to be included
95   * in output messages. Defaults to <code>false</code>.</li>
96   *
97   * <li><code>org.slf4j.simpleLogger.levelInBrackets</code> - Should the level
98   * string be output in brackets? Defaults to <code>false</code>.</li>
99   *
100  * <li><code>org.slf4j.simpleLogger.warnLevelString</code> - The string value
101  * output for the warn level. Defaults to <code>WARN</code>.</li>
102  * 
103  * </ul>
104  *
105  * <p>
106  * In addition to looking for system properties with the names specified above,
107  * this implementation also checks for a class loader resource named
108  * <code>"simplelogger.properties"</code>, and includes any matching definitions
109  * from this resource (if it exists).
110  * </p>
111  *
112  * <p>
113  * With no configuration, the default output includes the relative time in
114  * milliseconds, thread name, the level, logger name, and the message followed
115  * by the line separator for the host. In log4j terms it amounts to the "%r [%t]
116  * %level %logger - %m%n" pattern.
117  * </p>
118  * <p>
119  * Sample output follows.
120  * </p>
121  * 
122  * <pre>
123  * 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
124  * 225 [main] INFO examples.SortAlgo - Entered the sort method.
125  * 304 [main] INFO examples.SortAlgo - Dump of integer array:
126  * 317 [main] INFO examples.SortAlgo - Element [0] = 0
127  * 331 [main] INFO examples.SortAlgo - Element [1] = 1
128  * 343 [main] INFO examples.Sort - The next log statement should be an error message.
129  * 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array.
130  *   at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
131  *   at org.log4j.examples.Sort.main(Sort.java:64)
132  * 467 [main] INFO  examples.Sort - Exiting main method.
133  * </pre>
134  *
135  * <p>
136  * This implementation is heavily inspired by
137  * <a href="http://commons.apache.org/logging/">Apache Commons Logging</a>'s
138  * SimpleLog.
139  * </p>
140  *
141  * @author Ceki G&uuml;lc&uuml;
142  * @author Scott Sanders
143  * @author Rod Waldhoff
144  * @author Robert Burrell Donkin
145  * @author C&eacute;drik LIME
146  */
147 public class SimpleLogger extends MarkerIgnoringBase {
148 
149 
150 	private static final long serialVersionUID = -632788891211436180L;
151 
152     private static long START_TIME = System.currentTimeMillis();
153 
154     protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
155     protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
156     protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
157     protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
158     protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
159     
160     private static final String TID_PREFIX = "tid=";
161 
162     // The OFF level can only be used in configuration files to disable logging.
163     // It has
164     // no printing method associated with it in o.s.Logger interface.
165     protected static final int LOG_LEVEL_OFF = LOG_LEVEL_ERROR + 10;
166 
167     private static boolean INITIALIZED = false;
168     static final private SimpleLoggerConfiguration CONFIG_PARAMS =  new SimpleLoggerConfiguration();
169 
170     static void lazyInit() {
171         if (INITIALIZED) {
172             return;
173         }
174         INITIALIZED = true;
175         init();
176     }
177 
178     // external software might be invoking this method directly. Do not rename
179     // or change its semantics.
180     static void init() {
181         CONFIG_PARAMS.init();
182     }
183 
184     /** The current log level */
185     protected int currentLogLevel = LOG_LEVEL_INFO;
186     /** The short name of this simple log instance */
187     private transient String shortLogName = null;
188 
189     /**
190      * All system properties used by <code>SimpleLogger</code> start with this
191      * prefix
192      */
193     public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
194 
195     public static final String LOG_KEY_PREFIX = SimpleLogger.SYSTEM_PREFIX + "log.";
196 
197     public static final String CACHE_OUTPUT_STREAM_STRING_KEY = SimpleLogger.SYSTEM_PREFIX + "cacheOutputStream";
198 
199     public static final String WARN_LEVEL_STRING_KEY = SimpleLogger.SYSTEM_PREFIX + "warnLevelString";
200 
201     public static final String LEVEL_IN_BRACKETS_KEY = SimpleLogger.SYSTEM_PREFIX + "levelInBrackets";
202 
203     public static final String LOG_FILE_KEY = SimpleLogger.SYSTEM_PREFIX + "logFile";
204 
205     public static final String SHOW_SHORT_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showShortLogName";
206 
207     public static final String SHOW_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showLogName";
208 
209     public static final String SHOW_THREAD_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadName";
210 
211     public static final String SHOW_THREAD_ID_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadId";
212 
213     public static final String DATE_TIME_FORMAT_KEY = SimpleLogger.SYSTEM_PREFIX + "dateTimeFormat";
214 
215     public static final String SHOW_DATE_TIME_KEY = SimpleLogger.SYSTEM_PREFIX + "showDateTime";
216 
217     public static final String DEFAULT_LOG_LEVEL_KEY = SimpleLogger.SYSTEM_PREFIX + "defaultLogLevel";
218 
219     /**
220      * Package access allows only {@link SimpleLoggerFactory} to instantiate
221      * SimpleLogger instances.
222      */
223     SimpleLogger(String name) {
224         this.name = name;
225 
226         String levelString = recursivelyComputeLevelString();
227         if (levelString != null) {
228             this.currentLogLevel = SimpleLoggerConfiguration.stringToLevel(levelString);
229         } else {
230             this.currentLogLevel = CONFIG_PARAMS.defaultLogLevel;
231         }
232     }
233 
234     String recursivelyComputeLevelString() {
235         String tempName = name;
236         String levelString = null;
237         int indexOfLastDot = tempName.length();
238         while ((levelString == null) && (indexOfLastDot > -1)) {
239             tempName = tempName.substring(0, indexOfLastDot);
240             levelString = CONFIG_PARAMS.getStringProperty(SimpleLogger.LOG_KEY_PREFIX + tempName, null);
241             indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
242         }
243         return levelString;
244     }
245 
246     /**
247      * This is our internal implementation for logging regular
248      * (non-parameterized) log messages.
249      *
250      * @param level
251      *            One of the LOG_LEVEL_XXX constants defining the log level
252      * @param message
253      *            The message itself
254      * @param t
255      *            The exception whose stack trace should be logged
256      */
257     private void log(int level, String message, Throwable t) {
258         if (!isLevelEnabled(level)) {
259             return;
260         }
261 
262         StringBuilder buf = new StringBuilder(32);
263 
264         // Append date-time if so configured
265         if (CONFIG_PARAMS.showDateTime) {
266             if (CONFIG_PARAMS.dateFormatter != null) {
267                 buf.append(getFormattedDate());
268                 buf.append(' ');
269             } else {
270                 buf.append(System.currentTimeMillis() - START_TIME);
271                 buf.append(' ');
272             }
273         }
274 
275         // Append current thread name if so configured
276         if (CONFIG_PARAMS.showThreadName) {
277             buf.append('[');
278             buf.append(Thread.currentThread().getName());
279             buf.append("] ");
280         }
281 
282         if (CONFIG_PARAMS.showThreadId) {
283             buf.append(TID_PREFIX);
284             buf.append(Thread.currentThread().getId());
285             buf.append(' ');
286         }
287         
288         if (CONFIG_PARAMS.levelInBrackets)
289             buf.append('[');
290 
291         // Append a readable representation of the log level
292         String levelStr = renderLevel(level);
293         buf.append(levelStr);
294         if (CONFIG_PARAMS.levelInBrackets)
295             buf.append(']');
296         buf.append(' ');
297 
298         // Append the name of the log instance if so configured
299         if (CONFIG_PARAMS.showShortLogName) {
300             if (shortLogName == null)
301                 shortLogName = computeShortName();
302             buf.append(String.valueOf(shortLogName)).append(" - ");
303         } else if (CONFIG_PARAMS.showLogName) {
304             buf.append(String.valueOf(name)).append(" - ");
305         }
306 
307         // Append the message
308         buf.append(message);
309 
310         write(buf, t);
311 
312     }
313 
314     protected String renderLevel(int level) {
315         switch (level) {
316         case LOG_LEVEL_TRACE:
317             return "TRACE";
318         case LOG_LEVEL_DEBUG:
319             return ("DEBUG");
320         case LOG_LEVEL_INFO:
321             return "INFO";
322         case LOG_LEVEL_WARN:
323             return CONFIG_PARAMS.warnLevelString;
324         case LOG_LEVEL_ERROR:
325             return "ERROR";
326         }
327         throw new IllegalStateException("Unrecognized level [" + level + "]");
328     }
329 
330     /**
331      * To avoid intermingling of log messages and associated stack traces, the two
332      * operations are done in a synchronized block.
333      * 
334      * @param buf
335      * @param t
336      */
337     void write(StringBuilder buf, Throwable t) {
338         PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();
339 
340         synchronized (CONFIG_PARAMS) {
341             targetStream.println(buf.toString());
342             writeThrowable(t, targetStream);
343             targetStream.flush();
344         }
345     }
346 
347     protected void writeThrowable(Throwable t, PrintStream targetStream) {
348         if (t != null) {
349             t.printStackTrace(targetStream);
350         }
351     }
352 
353     private String getFormattedDate() {
354         Date now = new Date();
355         String dateText;
356         synchronized (CONFIG_PARAMS.dateFormatter) {
357             dateText = CONFIG_PARAMS.dateFormatter.format(now);
358         }
359         return dateText;
360     }
361 
362     private String computeShortName() {
363         return name.substring(name.lastIndexOf(".") + 1);
364     }
365 
366     /**
367      * For formatted messages, first substitute arguments and then log.
368      *
369      * @param level
370      * @param format
371      * @param arg1
372      * @param arg2
373      */
374     private void formatAndLog(int level, String format, Object arg1, Object arg2) {
375         if (!isLevelEnabled(level)) {
376             return;
377         }
378         FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
379         log(level, tp.getMessage(), tp.getThrowable());
380     }
381 
382     /**
383      * For formatted messages, first substitute arguments and then log.
384      *
385      * @param level
386      * @param format
387      * @param arguments
388      *            a list of 3 ore more arguments
389      */
390     private void formatAndLog(int level, String format, Object... arguments) {
391         if (!isLevelEnabled(level)) {
392             return;
393         }
394         FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
395         log(level, tp.getMessage(), tp.getThrowable());
396     }
397 
398     /**
399      * Is the given log level currently enabled?
400      *
401      * @param logLevel
402      *            is this level enabled?
403      */
404     protected boolean isLevelEnabled(int logLevel) {
405         // log level are numerically ordered so can use simple numeric
406         // comparison
407         return (logLevel >= currentLogLevel);
408     }
409 
410     /** Are {@code trace} messages currently enabled? */
411     public boolean isTraceEnabled() {
412         return isLevelEnabled(LOG_LEVEL_TRACE);
413     }
414 
415     /**
416      * A simple implementation which logs messages of level TRACE according to
417      * the format outlined above.
418      */
419     public void trace(String msg) {
420         log(LOG_LEVEL_TRACE, msg, null);
421     }
422 
423     /**
424      * Perform single parameter substitution before logging the message of level
425      * TRACE according to the format outlined above.
426      */
427     public void trace(String format, Object param1) {
428         formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
429     }
430 
431     /**
432      * Perform double parameter substitution before logging the message of level
433      * TRACE according to the format outlined above.
434      */
435     public void trace(String format, Object param1, Object param2) {
436         formatAndLog(LOG_LEVEL_TRACE, format, param1, param2);
437     }
438 
439     /**
440      * Perform double parameter substitution before logging the message of level
441      * TRACE according to the format outlined above.
442      */
443     public void trace(String format, Object... argArray) {
444         formatAndLog(LOG_LEVEL_TRACE, format, argArray);
445     }
446 
447     /** Log a message of level TRACE, including an exception. */
448     public void trace(String msg, Throwable t) {
449         log(LOG_LEVEL_TRACE, msg, t);
450     }
451 
452     /** Are {@code debug} messages currently enabled? */
453     public boolean isDebugEnabled() {
454         return isLevelEnabled(LOG_LEVEL_DEBUG);
455     }
456 
457     /**
458      * A simple implementation which logs messages of level DEBUG according to
459      * the format outlined above.
460      */
461     public void debug(String msg) {
462         log(LOG_LEVEL_DEBUG, msg, null);
463     }
464 
465     /**
466      * Perform single parameter substitution before logging the message of level
467      * DEBUG according to the format outlined above.
468      */
469     public void debug(String format, Object param1) {
470         formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
471     }
472 
473     /**
474      * Perform double parameter substitution before logging the message of level
475      * DEBUG according to the format outlined above.
476      */
477     public void debug(String format, Object param1, Object param2) {
478         formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2);
479     }
480 
481     /**
482      * Perform double parameter substitution before logging the message of level
483      * DEBUG according to the format outlined above.
484      */
485     public void debug(String format, Object... argArray) {
486         formatAndLog(LOG_LEVEL_DEBUG, format, argArray);
487     }
488 
489     /** Log a message of level DEBUG, including an exception. */
490     public void debug(String msg, Throwable t) {
491         log(LOG_LEVEL_DEBUG, msg, t);
492     }
493 
494     /** Are {@code info} messages currently enabled? */
495     public boolean isInfoEnabled() {
496         return isLevelEnabled(LOG_LEVEL_INFO);
497     }
498 
499     /**
500      * A simple implementation which logs messages of level INFO according to
501      * the format outlined above.
502      */
503     public void info(String msg) {
504         log(LOG_LEVEL_INFO, msg, null);
505     }
506 
507     /**
508      * Perform single parameter substitution before logging the message of level
509      * INFO according to the format outlined above.
510      */
511     public void info(String format, Object arg) {
512         formatAndLog(LOG_LEVEL_INFO, format, arg, null);
513     }
514 
515     /**
516      * Perform double parameter substitution before logging the message of level
517      * INFO according to the format outlined above.
518      */
519     public void info(String format, Object arg1, Object arg2) {
520         formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2);
521     }
522 
523     /**
524      * Perform double parameter substitution before logging the message of level
525      * INFO according to the format outlined above.
526      */
527     public void info(String format, Object... argArray) {
528         formatAndLog(LOG_LEVEL_INFO, format, argArray);
529     }
530 
531     /** Log a message of level INFO, including an exception. */
532     public void info(String msg, Throwable t) {
533         log(LOG_LEVEL_INFO, msg, t);
534     }
535 
536     /** Are {@code warn} messages currently enabled? */
537     public boolean isWarnEnabled() {
538         return isLevelEnabled(LOG_LEVEL_WARN);
539     }
540 
541     /**
542      * A simple implementation which always logs messages of level WARN
543      * according to the format outlined above.
544      */
545     public void warn(String msg) {
546         log(LOG_LEVEL_WARN, msg, null);
547     }
548 
549     /**
550      * Perform single parameter substitution before logging the message of level
551      * WARN according to the format outlined above.
552      */
553     public void warn(String format, Object arg) {
554         formatAndLog(LOG_LEVEL_WARN, format, arg, null);
555     }
556 
557     /**
558      * Perform double parameter substitution before logging the message of level
559      * WARN according to the format outlined above.
560      */
561     public void warn(String format, Object arg1, Object arg2) {
562         formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2);
563     }
564 
565     /**
566      * Perform double parameter substitution before logging the message of level
567      * WARN according to the format outlined above.
568      */
569     public void warn(String format, Object... argArray) {
570         formatAndLog(LOG_LEVEL_WARN, format, argArray);
571     }
572 
573     /** Log a message of level WARN, including an exception. */
574     public void warn(String msg, Throwable t) {
575         log(LOG_LEVEL_WARN, msg, t);
576     }
577 
578     /** Are {@code error} messages currently enabled? */
579     public boolean isErrorEnabled() {
580         return isLevelEnabled(LOG_LEVEL_ERROR);
581     }
582 
583     /**
584      * A simple implementation which always logs messages of level ERROR
585      * according to the format outlined above.
586      */
587     public void error(String msg) {
588         log(LOG_LEVEL_ERROR, msg, null);
589     }
590 
591     /**
592      * Perform single parameter substitution before logging the message of level
593      * ERROR according to the format outlined above.
594      */
595     public void error(String format, Object arg) {
596         formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
597     }
598 
599     /**
600      * Perform double parameter substitution before logging the message of level
601      * ERROR according to the format outlined above.
602      */
603     public void error(String format, Object arg1, Object arg2) {
604         formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2);
605     }
606 
607     /**
608      * Perform double parameter substitution before logging the message of level
609      * ERROR according to the format outlined above.
610      */
611     public void error(String format, Object... argArray) {
612         formatAndLog(LOG_LEVEL_ERROR, format, argArray);
613     }
614 
615     /** Log a message of level ERROR, including an exception. */
616     public void error(String msg, Throwable t) {
617         log(LOG_LEVEL_ERROR, msg, t);
618     }
619 
620     public void log(LoggingEvent event) {
621         int levelInt = event.getLevel().toInt();
622 
623         if (!isLevelEnabled(levelInt)) {
624             return;
625         }
626         FormattingTuple tp = MessageFormatter.arrayFormat(event.getMessage(), event.getArgumentArray(), event.getThrowable());
627         log(levelInt, tp.getMessage(), event.getThrowable());
628     }
629 
630 }