Class SimpleLogger
- All Implemented Interfaces:
Serializable,Logger
Simple implementation of Logger that sends all enabled log messages,
for all defined loggers, to the console (System.err). The following
system properties are supported to configure the behavior of this logger:
org.slf4j.simpleLogger.logFile- The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err".org.slf4j.simpleLogger.cacheOutputStream- If the output target is set to "System.out" or "System.err" (see preceding entry), by default, logs will be output to the latest value referenced bySystem.out/errvariables. By setting this parameter to true, the output stream will be cached, i.e. assigned once at initialization time and re-used independently of the current value referenced bySystem.out/err.org.slf4j.simpleLogger.defaultLogLevel- Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", "error" or "off"). If not specified, defaults to "info".org.slf4j.simpleLogger.log.a.b.c- Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified byorg.slf4j.simpleLogger.defaultLogLevelwill be used.org.slf4j.simpleLogger.showDateTime- Set totrueif you want the current date and time to be included in output messages. Default isfalseorg.slf4j.simpleLogger.dateTimeFormat- The date and time format to be used in the output messages. The pattern describing the date and time format is defined bySimpleDateFormat. If the format is not specified or is invalid, the number of milliseconds since start up will be output.org.slf4j.simpleLogger.showThreadName-Set totrueif you want to output the current thread name. Defaults totrue.org.slf4j.simpleLogger.showLogName- Set totrueif you want the Logger instance name to be included in output messages. Defaults totrue.org.slf4j.simpleLogger.showShortLogName- Set totrueif you want the last component of the name to be included in output messages. Defaults tofalse.org.slf4j.simpleLogger.levelInBrackets- Should the level string be output in brackets? Defaults tofalse.org.slf4j.simpleLogger.warnLevelString- The string value output for the warn level. Defaults toWARN.
In addition to looking for system properties with the names specified above,
this implementation also checks for a class loader resource named
"simplelogger.properties", and includes any matching definitions
from this resource (if it exists).
With no configuration, the default output includes the relative time in milliseconds, thread name, the level, logger name, and the message followed by the line separator for the host. In log4j terms it amounts to the "%r [%t] %level %logger - %m%n" pattern.
Sample output follows.
176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order. 225 [main] INFO examples.SortAlgo - Entered the sort method. 304 [main] INFO examples.SortAlgo - Dump of integer array: 317 [main] INFO examples.SortAlgo - Element [0] = 0 331 [main] INFO examples.SortAlgo - Element [1] = 1 343 [main] INFO examples.Sort - The next log statement should be an error message. 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array. at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58) at org.log4j.examples.Sort.main(Sort.java:64) 467 [main] INFO examples.Sort - Exiting main method.
This implementation is heavily inspired by Apache Commons Logging's SimpleLog.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringprivate static final SimpleLoggerConfigurationprotected intThe current log levelstatic final Stringstatic final Stringprivate static booleanstatic final Stringstatic final Stringstatic final Stringprotected static final intprotected static final intprotected static final intprotected static final intprotected static final intprotected static final intprivate static final longprivate StringThe short name of this simple log instancestatic final Stringstatic final Stringstatic final Stringstatic final Stringprivate static longstatic final StringAll system properties used bySimpleLoggerstart with this prefixstatic final StringFields inherited from class org.slf4j.helpers.NamedLoggerBase
nameFields inherited from interface org.slf4j.Logger
ROOT_LOGGER_NAME -
Constructor Summary
ConstructorsConstructorDescriptionSimpleLogger(String name) Package access allows onlySimpleLoggerFactoryto instantiate SimpleLogger instances. -
Method Summary
Modifier and TypeMethodDescriptionprivate StringvoidA simple implementation which logs messages of level DEBUG according to the format outlined above.voidPerform single parameter substitution before logging the message of level DEBUG according to the format outlined above.voidPerform double parameter substitution before logging the message of level DEBUG according to the format outlined above.voidPerform double parameter substitution before logging the message of level DEBUG according to the format outlined above.voidLog a message of level DEBUG, including an exception.voidA simple implementation which always logs messages of level ERROR according to the format outlined above.voidPerform single parameter substitution before logging the message of level ERROR according to the format outlined above.voidPerform double parameter substitution before logging the message of level ERROR according to the format outlined above.voidPerform double parameter substitution before logging the message of level ERROR according to the format outlined above.voidLog a message of level ERROR, including an exception.private voidformatAndLog(int level, String format, Object... arguments) For formatted messages, first substitute arguments and then log.private voidformatAndLog(int level, String format, Object arg1, Object arg2) For formatted messages, first substitute arguments and then log.private StringvoidA simple implementation which logs messages of level INFO according to the format outlined above.voidPerform single parameter substitution before logging the message of level INFO according to the format outlined above.voidPerform double parameter substitution before logging the message of level INFO according to the format outlined above.voidPerform double parameter substitution before logging the message of level INFO according to the format outlined above.voidLog a message of level INFO, including an exception.(package private) static voidinit()booleanAredebugmessages currently enabled?booleanAreerrormessages currently enabled?booleanAreinfomessages currently enabled?protected booleanisLevelEnabled(int logLevel) Is the given log level currently enabled?booleanAretracemessages currently enabled?booleanArewarnmessages currently enabled?(package private) static voidlazyInit()private voidThis is our internal implementation for logging regular (non-parameterized) log messages.voidlog(LoggingEvent event) (package private) Stringprotected StringrenderLevel(int level) voidA simple implementation which logs messages of level TRACE according to the format outlined above.voidPerform single parameter substitution before logging the message of level TRACE according to the format outlined above.voidPerform double parameter substitution before logging the message of level TRACE according to the format outlined above.voidPerform double parameter substitution before logging the message of level TRACE according to the format outlined above.voidLog a message of level TRACE, including an exception.voidA simple implementation which always logs messages of level WARN according to the format outlined above.voidPerform single parameter substitution before logging the message of level WARN according to the format outlined above.voidPerform double parameter substitution before logging the message of level WARN according to the format outlined above.voidPerform double parameter substitution before logging the message of level WARN according to the format outlined above.voidLog a message of level WARN, including an exception.(package private) voidwrite(StringBuilder buf, Throwable t) To avoid intermingling of log messages and associated stack traces, the two operations are done in a synchronized block.protected voidwriteThrowable(Throwable t, PrintStream targetStream) Methods inherited from class org.slf4j.helpers.MarkerIgnoringBase
debug, debug, debug, debug, debug, error, error, error, error, error, info, info, info, info, info, isDebugEnabled, isErrorEnabled, isInfoEnabled, isTraceEnabled, isWarnEnabled, toString, trace, trace, trace, trace, trace, warn, warn, warn, warn, warnMethods inherited from class org.slf4j.helpers.NamedLoggerBase
getName, readResolve
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
START_TIME
private static long START_TIME -
LOG_LEVEL_TRACE
protected static final int LOG_LEVEL_TRACE- See Also:
-
LOG_LEVEL_DEBUG
protected static final int LOG_LEVEL_DEBUG- See Also:
-
LOG_LEVEL_INFO
protected static final int LOG_LEVEL_INFO- See Also:
-
LOG_LEVEL_WARN
protected static final int LOG_LEVEL_WARN- See Also:
-
LOG_LEVEL_ERROR
protected static final int LOG_LEVEL_ERROR- See Also:
-
LOG_LEVEL_OFF
protected static final int LOG_LEVEL_OFF- See Also:
-
INITIALIZED
private static boolean INITIALIZED -
CONFIG_PARAMS
-
currentLogLevel
protected int currentLogLevelThe current log level -
shortLogName
The short name of this simple log instance -
SYSTEM_PREFIX
All system properties used bySimpleLoggerstart with this prefix- See Also:
-
LOG_KEY_PREFIX
- See Also:
-
CACHE_OUTPUT_STREAM_STRING_KEY
- See Also:
-
WARN_LEVEL_STRING_KEY
- See Also:
-
LEVEL_IN_BRACKETS_KEY
- See Also:
-
LOG_FILE_KEY
- See Also:
-
SHOW_SHORT_LOG_NAME_KEY
- See Also:
-
SHOW_LOG_NAME_KEY
- See Also:
-
SHOW_THREAD_NAME_KEY
- See Also:
-
DATE_TIME_FORMAT_KEY
- See Also:
-
SHOW_DATE_TIME_KEY
- See Also:
-
DEFAULT_LOG_LEVEL_KEY
- See Also:
-
-
Constructor Details
-
SimpleLogger
SimpleLogger(String name) Package access allows onlySimpleLoggerFactoryto instantiate SimpleLogger instances.
-
-
Method Details
-
lazyInit
static void lazyInit() -
init
static void init() -
recursivelyComputeLevelString
String recursivelyComputeLevelString() -
log
This is our internal implementation for logging regular (non-parameterized) log messages.- Parameters:
level- One of the LOG_LEVEL_XXX constants defining the log levelmessage- The message itselft- The exception whose stack trace should be logged
-
renderLevel
-
write
To avoid intermingling of log messages and associated stack traces, the two operations are done in a synchronized block.- Parameters:
buf-t-
-
writeThrowable
-
getFormattedDate
-
computeShortName
-
formatAndLog
For formatted messages, first substitute arguments and then log.- Parameters:
level-format-arg1-arg2-
-
formatAndLog
For formatted messages, first substitute arguments and then log.- Parameters:
level-format-arguments- a list of 3 ore more arguments
-
isLevelEnabled
protected boolean isLevelEnabled(int logLevel) Is the given log level currently enabled?- Parameters:
logLevel- is this level enabled?
-
isTraceEnabled
public boolean isTraceEnabled()Aretracemessages currently enabled?- Returns:
- True if this Logger is enabled for the TRACE level, false otherwise.
-
trace
A simple implementation which logs messages of level TRACE according to the format outlined above.- Parameters:
msg- the message string to be logged
-
trace
Perform single parameter substitution before logging the message of level TRACE according to the format outlined above.- Parameters:
format- the format stringparam1- the argument
-
trace
Perform double parameter substitution before logging the message of level TRACE according to the format outlined above.- Parameters:
format- the format stringparam1- the first argumentparam2- the second argument
-
trace
Perform double parameter substitution before logging the message of level TRACE according to the format outlined above.- Parameters:
format- the format stringargArray- a list of 3 or more arguments
-
trace
Log a message of level TRACE, including an exception.- Parameters:
msg- the message accompanying the exceptiont- the exception (throwable) to log
-
isDebugEnabled
public boolean isDebugEnabled()Aredebugmessages currently enabled?- Returns:
- True if this Logger is enabled for the DEBUG level, false otherwise.
-
debug
A simple implementation which logs messages of level DEBUG according to the format outlined above.- Parameters:
msg- the message string to be logged
-
debug
Perform single parameter substitution before logging the message of level DEBUG according to the format outlined above.- Parameters:
format- the format stringparam1- the argument
-
debug
Perform double parameter substitution before logging the message of level DEBUG according to the format outlined above.- Parameters:
format- the format stringparam1- the first argumentparam2- the second argument
-
debug
Perform double parameter substitution before logging the message of level DEBUG according to the format outlined above.- Parameters:
format- the format stringargArray- a list of 3 or more arguments
-
debug
Log a message of level DEBUG, including an exception.- Parameters:
msg- the message accompanying the exceptiont- the exception (throwable) to log
-
isInfoEnabled
public boolean isInfoEnabled()Areinfomessages currently enabled?- Returns:
- True if this Logger is enabled for the INFO level, false otherwise.
-
info
A simple implementation which logs messages of level INFO according to the format outlined above.- Parameters:
msg- the message string to be logged
-
info
Perform single parameter substitution before logging the message of level INFO according to the format outlined above.- Parameters:
format- the format stringarg- the argument
-
info
Perform double parameter substitution before logging the message of level INFO according to the format outlined above.- Parameters:
format- the format stringarg1- the first argumentarg2- the second argument
-
info
Perform double parameter substitution before logging the message of level INFO according to the format outlined above.- Parameters:
format- the format stringargArray- a list of 3 or more arguments
-
info
Log a message of level INFO, including an exception.- Parameters:
msg- the message accompanying the exceptiont- the exception (throwable) to log
-
isWarnEnabled
public boolean isWarnEnabled()Arewarnmessages currently enabled?- Returns:
- True if this Logger is enabled for the WARN level, false otherwise.
-
warn
A simple implementation which always logs messages of level WARN according to the format outlined above.- Parameters:
msg- the message string to be logged
-
warn
Perform single parameter substitution before logging the message of level WARN according to the format outlined above.- Parameters:
format- the format stringarg- the argument
-
warn
Perform double parameter substitution before logging the message of level WARN according to the format outlined above.- Parameters:
format- the format stringarg1- the first argumentarg2- the second argument
-
warn
Perform double parameter substitution before logging the message of level WARN according to the format outlined above.- Parameters:
format- the format stringargArray- a list of 3 or more arguments
-
warn
Log a message of level WARN, including an exception.- Parameters:
msg- the message accompanying the exceptiont- the exception (throwable) to log
-
isErrorEnabled
public boolean isErrorEnabled()Areerrormessages currently enabled?- Returns:
- True if this Logger is enabled for the ERROR level, false otherwise.
-
error
A simple implementation which always logs messages of level ERROR according to the format outlined above.- Parameters:
msg- the message string to be logged
-
error
Perform single parameter substitution before logging the message of level ERROR according to the format outlined above.- Parameters:
format- the format stringarg- the argument
-
error
Perform double parameter substitution before logging the message of level ERROR according to the format outlined above.- Parameters:
format- the format stringarg1- the first argumentarg2- the second argument
-
error
Perform double parameter substitution before logging the message of level ERROR according to the format outlined above.- Parameters:
format- the format stringargArray- a list of 3 or more arguments
-
error
Log a message of level ERROR, including an exception.- Parameters:
msg- the message accompanying the exceptiont- the exception (throwable) to log
-
log
-