View Javadoc
1   /**
2    * Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
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.simple;
26  
27  import java.io.PrintStream;
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.List;
31  
32  import org.slf4j.Logger;
33  import org.slf4j.Marker;
34  import org.slf4j.event.Level;
35  import org.slf4j.event.LoggingEvent;
36  import org.slf4j.helpers.LegacyAbstractLogger;
37  import org.slf4j.helpers.MessageFormatter;
38  import org.slf4j.helpers.NormalizedParameters;
39  import org.slf4j.spi.LocationAwareLogger;
40  
41  /**
42   * <p>
43   * Simple implementation of {@link Logger} that sends all enabled log messages,
44   * for all defined loggers, to the console ({@code System.err}). The following
45   * system properties are supported to configure the behavior of this logger:
46   * 
47   *
48   * <ul>
49   * <li><code>org.slf4j.simpleLogger.logFile</code> - The output target which can
50   * be the <em>path</em> to a file, or the special values "System.out" and
51   * "System.err". Default is "System.err".</li>
52   * 
53   * <li><code>org.slf4j.simpleLogger.cacheOutputStream</code> - If the output
54   * target is set to "System.out" or "System.err" (see preceding entry), by
55   * default, logs will be output to the latest value referenced by
56   * <code>System.out/err</code> variables. By setting this parameter to true, the
57   * output stream will be cached, i.e. assigned once at initialization time and
58   * re-used independently of the current value referenced by
59   * <code>System.out/err</code>.</li>
60   * 
61   * <li><code>org.slf4j.simpleLogger.defaultLogLevel</code> - Default log level
62   * for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
63   * "warn", "error" or "off"). If not specified, defaults to "info".</li>
64   *
65   * <li><code>org.slf4j.simpleLogger.log.<em>a.b.c</em></code> - Logging detail
66   * level for a SimpleLogger instance named "a.b.c". Right-side value must be one
67   * of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger
68   * named "a.b.c" is initialized, its level is assigned from this property. If
69   * unspecified, the level of nearest parent logger will be used, and if none is
70   * set, then the value specified by
71   * <code>org.slf4j.simpleLogger.defaultLogLevel</code> will be used.</li>
72   *
73   * <li><code>org.slf4j.simpleLogger.showDateTime</code> - Set to
74   * <code>true</code> if you want the current date and time to be included in
75   * output messages. Default is <code>false</code></li>
76   *
77   * <li><code>org.slf4j.simpleLogger.dateTimeFormat</code> - The date and time
78   * format to be used in the output messages. The pattern describing the date and
79   * time format is defined by <a href=
80   * "http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html">
81   * <code>SimpleDateFormat</code></a>. If the format is not specified or is
82   * invalid, the number of milliseconds since start up will be output.</li>
83   *
84   * <li><code>org.slf4j.simpleLogger.showThreadName</code> -Set to
85   * <code>true</code> if you want to output the current thread name. Defaults to
86   * <code>true</code>.</li>
87   * 
88   * <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> - 
89   * If you would like to output the current thread id, then set to
90   * <code>true</code>. Defaults to <code>false</code>.</li>
91   * 
92   * <li><code>org.slf4j.simpleLogger.showLogName</code> - Set to
93   * <code>true</code> if you want the Logger instance name to be included in
94   * output messages. Defaults to <code>true</code>.</li>
95   *
96   * <li><code>org.slf4j.simpleLogger.showShortLogName</code> - Set to
97   * <code>true</code> if you want the last component of the name to be included
98   * in output messages. Defaults to <code>false</code>.</li>
99   *
100  * <li><code>org.slf4j.simpleLogger.levelInBrackets</code> - Should the level
101  * string be output in brackets? Defaults to <code>false</code>.</li>
102  *
103  * <li><code>org.slf4j.simpleLogger.warnLevelString</code> - The string value
104  * output for the warn level. Defaults to <code>WARN</code>.</li>
105  * 
106  * </ul>
107  *
108  * <p>
109  * In addition to looking for system properties with the names specified above,
110  * this implementation also checks for a class loader resource named
111  * <code>"simplelogger.properties"</code>, and includes any matching definitions
112  * from this resource (if it exists).
113  * 
114  *
115  * <p>
116  * With no configuration, the default output includes the relative time in
117  * milliseconds, thread name, the level, logger name, and the message followed
118  * by the line separator for the host. In log4j terms it amounts to the "%r [%t]
119  * %level %logger - %m%n" pattern.
120  * 
121  * <p>
122  * Sample output follows.
123  * 
124  * 
125  * <pre>
126  * 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
127  * 225 [main] INFO examples.SortAlgo - Entered the sort method.
128  * 304 [main] INFO examples.SortAlgo - Dump of integer array:
129  * 317 [main] INFO examples.SortAlgo - Element [0] = 0
130  * 331 [main] INFO examples.SortAlgo - Element [1] = 1
131  * 343 [main] INFO examples.Sort - The next log statement should be an error message.
132  * 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array.
133  *   at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
134  *   at org.log4j.examples.Sort.main(Sort.java:64)
135  * 467 [main] INFO  examples.Sort - Exiting main method.
136  * </pre>
137  *
138  * <p>
139  * This implementation is heavily inspired by
140  * <a href="http://commons.apache.org/logging/">Apache Commons Logging</a>'s
141  * SimpleLog.
142  * 
143  *
144  * @author Ceki G&uuml;lc&uuml;
145  * @author Scott Sanders
146  * @author Rod Waldhoff
147  * @author Robert Burrell Donkin
148  * @author C&eacute;drik LIME
149  */
150 public class SimpleLogger extends LegacyAbstractLogger {
151 
152     private static final long serialVersionUID = -632788891211436180L;
153 
154     private static final long START_TIME = System.currentTimeMillis();
155 
156     protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
157     protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
158     protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
159     protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
160     protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
161 
162     static char SP = ' ';
163     static final String TID_PREFIX = "tid=";
164 
165 
166     // The OFF level can only be used in configuration files to disable logging.
167     // It has
168     // no printing method associated with it in o.s.Logger interface.
169     protected static final int LOG_LEVEL_OFF = LOG_LEVEL_ERROR + 10;
170 
171     private static boolean INITIALIZED = false;
172     static final SimpleLoggerConfiguration CONFIG_PARAMS = new SimpleLoggerConfiguration();
173     
174     static void lazyInit() {
175         if (INITIALIZED) {
176             return;
177         }
178         INITIALIZED = true;
179         init();
180     }
181 
182     // external software might be invoking this method directly. Do not rename
183     // or change its semantics.
184     static void init() {
185         CONFIG_PARAMS.init();
186     }
187 
188     /** The current log level */
189     protected int currentLogLevel = LOG_LEVEL_INFO;
190     /** The short name of this simple log instance */
191     private transient String shortLogName = null;
192 
193     /**
194      * All system properties used by <code>SimpleLogger</code> start with this
195      * prefix
196      */
197     public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
198 
199     public static final String LOG_KEY_PREFIX = SimpleLogger.SYSTEM_PREFIX + "log.";
200 
201     public static final String CACHE_OUTPUT_STREAM_STRING_KEY = SimpleLogger.SYSTEM_PREFIX + "cacheOutputStream";
202 
203     public static final String WARN_LEVEL_STRING_KEY = SimpleLogger.SYSTEM_PREFIX + "warnLevelString";
204 
205     public static final String LEVEL_IN_BRACKETS_KEY = SimpleLogger.SYSTEM_PREFIX + "levelInBrackets";
206 
207     public static final String LOG_FILE_KEY = SimpleLogger.SYSTEM_PREFIX + "logFile";
208 
209     public static final String SHOW_SHORT_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showShortLogName";
210 
211     public static final String SHOW_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showLogName";
212 
213     public static final String SHOW_THREAD_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadName";
214 
215     public static final String SHOW_THREAD_ID_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadId";
216     
217     public static final String DATE_TIME_FORMAT_KEY = SimpleLogger.SYSTEM_PREFIX + "dateTimeFormat";
218 
219     public static final String SHOW_DATE_TIME_KEY = SimpleLogger.SYSTEM_PREFIX + "showDateTime";
220 
221     public static final String DEFAULT_LOG_LEVEL_KEY = SimpleLogger.SYSTEM_PREFIX + "defaultLogLevel";
222 
223     /**
224      * Protected access allows only {@link SimpleLoggerFactory} and also derived classes to instantiate
225      * SimpleLogger instances.
226      */
227     protected SimpleLogger(String name) {
228         this.name = name;
229 
230         String levelString = recursivelyComputeLevelString();
231         if (levelString != null) {
232             this.currentLogLevel = SimpleLoggerConfiguration.stringToLevel(levelString);
233         } else {
234             this.currentLogLevel = CONFIG_PARAMS.defaultLogLevel;
235         }
236     }
237 
238     String recursivelyComputeLevelString() {
239         String tempName = name;
240         String levelString = null;
241         int indexOfLastDot = tempName.length();
242         while ((levelString == null) && (indexOfLastDot > -1)) {
243             tempName = tempName.substring(0, indexOfLastDot);
244             levelString = CONFIG_PARAMS.getStringProperty(SimpleLogger.LOG_KEY_PREFIX + tempName, null);
245             indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
246         }
247         return levelString;
248     }
249 
250     /**
251      * To avoid intermingling of log messages and associated stack traces, the two
252      * operations are done in a synchronized block.
253      * 
254      * @param buf
255      * @param t
256      */
257     void write(StringBuilder buf, Throwable t) {
258         PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();
259 
260         synchronized (CONFIG_PARAMS) {
261             targetStream.println(buf.toString());
262             writeThrowable(t, targetStream);
263             targetStream.flush();
264         } 
265 
266     }
267 
268     protected void writeThrowable(Throwable t, PrintStream targetStream) {
269         if (t != null) {
270             t.printStackTrace(targetStream);
271         }
272     }
273 
274     private String getFormattedDate() {
275         Date now = new Date();
276         String dateText;
277         synchronized (CONFIG_PARAMS.dateFormatter) {
278             dateText = CONFIG_PARAMS.dateFormatter.format(now);
279         }
280         return dateText;
281     }
282 
283     private String computeShortName() {
284         return name.substring(name.lastIndexOf(".") + 1);
285     }
286 
287     // /**
288     // * For formatted messages, first substitute arguments and then log.
289     // *
290     // * @param level
291     // * @param format
292     // * @param arg1
293     // * @param arg2
294     // */
295     // private void formatAndLog(int level, String format, Object arg1, Object arg2) {
296     // if (!isLevelEnabled(level)) {
297     // return;
298     // }
299     // FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
300     // log(level, tp.getMessage(), tp.getThrowable());
301     // }
302 
303     // /**
304     // * For formatted messages, first substitute arguments and then log.
305     // *
306     // * @param level
307     // * @param format
308     // * @param arguments
309     // * a list of 3 ore more arguments
310     // */
311     // private void formatAndLog(int level, String format, Object... arguments) {
312     // if (!isLevelEnabled(level)) {
313     // return;
314     // }
315     // FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
316     // log(level, tp.getMessage(), tp.getThrowable());
317     // }
318 
319     /**
320      * Is the given log level currently enabled?
321      *
322      * @param logLevel is this level enabled?
323      * @return whether the logger is enabled for the given level
324      */
325     protected boolean isLevelEnabled(int logLevel) {
326         // log level are numerically ordered so can use simple numeric
327         // comparison
328         return (logLevel >= currentLogLevel);
329     }
330 
331     /** Are {@code trace} messages currently enabled? */
332     public boolean isTraceEnabled() {
333         return isLevelEnabled(LOG_LEVEL_TRACE);
334     }
335 
336     /** Are {@code debug} messages currently enabled? */
337     public boolean isDebugEnabled() {
338         return isLevelEnabled(LOG_LEVEL_DEBUG);
339     }
340 
341     /** Are {@code info} messages currently enabled? */
342     public boolean isInfoEnabled() {
343         return isLevelEnabled(LOG_LEVEL_INFO);
344     }
345 
346     /** Are {@code warn} messages currently enabled? */
347     public boolean isWarnEnabled() {
348         return isLevelEnabled(LOG_LEVEL_WARN);
349     }
350 
351     /** Are {@code error} messages currently enabled? */
352     public boolean isErrorEnabled() {
353         return isLevelEnabled(LOG_LEVEL_ERROR);
354     }
355 
356     /**
357      * SimpleLogger's implementation of
358      * {@link org.slf4j.helpers.AbstractLogger#handleNormalizedLoggingCall(Level, Marker, String, Object[], Throwable) AbstractLogger#handleNormalizedLoggingCall}
359      * }
360      *
361      * @param level the SLF4J level for this event
362      * @param marker  The marker to be used for this event, may be null.
363      * @param messagePattern The message pattern which will be parsed and formatted
364      * @param arguments  the array of arguments to be formatted, may be null
365      * @param throwable  The exception whose stack trace should be logged, may be null
366      */
367     @Override
368     protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable) {
369 
370         List<Marker> markers = null;
371 
372         if (marker != null) {
373             markers = new ArrayList<>();
374             markers.add(marker);
375         }
376 
377         innerHandleNormalizedLoggingCall(level, markers, messagePattern, arguments, throwable);
378     }
379 
380     private void innerHandleNormalizedLoggingCall(Level level, List<Marker> markers, String messagePattern, Object[] arguments, Throwable t) {
381 
382         StringBuilder buf = new StringBuilder(32);
383 
384         // Append date-time if so configured
385         if (CONFIG_PARAMS.showDateTime) {
386             if (CONFIG_PARAMS.dateFormatter != null) {
387                 buf.append(getFormattedDate());
388                 buf.append(SP);
389             } else {
390                 buf.append(System.currentTimeMillis() - START_TIME);
391                 buf.append(SP);
392             }
393         }
394 
395         // Append current thread name if so configured
396         if (CONFIG_PARAMS.showThreadName) {
397             buf.append('[');
398             buf.append(Thread.currentThread().getName());
399             buf.append("] ");
400         }
401         
402         if (CONFIG_PARAMS.showThreadId) {
403             buf.append(TID_PREFIX);
404             buf.append(Thread.currentThread().getId());
405             buf.append(SP);
406         }
407 
408         if (CONFIG_PARAMS.levelInBrackets)
409             buf.append('[');
410 
411         // Append a readable representation of the log level
412         String levelStr = renderLevel(level.toInt());
413         buf.append(levelStr);
414         if (CONFIG_PARAMS.levelInBrackets)
415             buf.append(']');
416         buf.append(SP);
417 
418         // Append the name of the log instance if so configured
419         if (CONFIG_PARAMS.showShortLogName) {
420             if (shortLogName == null)
421                 shortLogName = computeShortName();
422             buf.append(String.valueOf(shortLogName)).append(" - ");
423         } else if (CONFIG_PARAMS.showLogName) {
424             buf.append(String.valueOf(name)).append(" - ");
425         }
426 
427         if (markers != null) {
428             buf.append(SP);
429             for (Marker marker : markers) {
430                 buf.append(marker.getName()).append(SP);
431             }
432         }
433 
434         String formattedMessage = MessageFormatter.basicArrayFormat(messagePattern, arguments);
435 
436         // Append the message
437         buf.append(formattedMessage);
438 
439         write(buf, t);
440     }
441 
442     protected String renderLevel(int levelInt) {
443         switch (levelInt) {
444             case LOG_LEVEL_TRACE:
445                 return "TRACE";
446             case LOG_LEVEL_DEBUG:
447                 return("DEBUG");
448             case LOG_LEVEL_INFO:
449                 return "INFO";
450             case LOG_LEVEL_WARN:
451                 return "WARN";
452             case LOG_LEVEL_ERROR:
453                 return "ERROR";
454         }
455         throw new IllegalStateException("Unrecognized level ["+levelInt+"]");
456     }
457 
458     public void log(LoggingEvent event) {
459         int levelInt = event.getLevel().toInt();
460 
461         if (!isLevelEnabled(levelInt)) {
462             return;
463         }
464 
465         NormalizedParameters np = NormalizedParameters.normalize(event);
466 
467         innerHandleNormalizedLoggingCall(event.getLevel(), event.getMarkers(), np.getMessage(), np.getArguments(), event.getThrowable());
468     }
469 
470     @Override
471     protected String getFullyQualifiedCallerName() {
472         return null;
473     }
474 
475 }