public class SimpleLogger extends MarkerIgnoringBase
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 by
System.out/err
variables. 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 by
System.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 by
org.slf4j.simpleLogger.defaultLogLevel
will be used.org.slf4j.simpleLogger.showDateTime
- Set to
true
if you want the current date and time to be included in
output messages. Default is false
org.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 by
SimpleDateFormat
. If the format is not specified or is
invalid, the number of milliseconds since start up will be output.org.slf4j.simpleLogger.showThreadName
-Set to
true
if you want to output the current thread name. Defaults to
true
.org.slf4j.simpleLogger.showLogName
- Set to
true
if you want the Logger instance name to be included in
output messages. Defaults to true
.org.slf4j.simpleLogger.showShortLogName
- Set to
true
if you want the last component of the name to be included
in output messages. Defaults to false
.org.slf4j.simpleLogger.levelInBrackets
- Should the level
string be output in brackets? Defaults to false
.org.slf4j.simpleLogger.warnLevelString
- The string value
output for the warn level. Defaults to WARN
.
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CACHE_OUTPUT_STREAM_STRING_KEY |
(package private) static SimpleLoggerConfiguration |
CONFIG_PARAMS |
protected int |
currentLogLevel
The current log level
|
static java.lang.String |
DATE_TIME_FORMAT_KEY |
static java.lang.String |
DEFAULT_LOG_LEVEL_KEY |
private static boolean |
INITIALIZED |
static java.lang.String |
LEVEL_IN_BRACKETS_KEY |
static java.lang.String |
LOG_FILE_KEY |
static java.lang.String |
LOG_KEY_PREFIX |
protected static int |
LOG_LEVEL_DEBUG |
protected static int |
LOG_LEVEL_ERROR |
protected static int |
LOG_LEVEL_INFO |
protected static int |
LOG_LEVEL_OFF |
protected static int |
LOG_LEVEL_TRACE |
protected static int |
LOG_LEVEL_WARN |
private static long |
serialVersionUID |
private java.lang.String |
shortLogName
The short name of this simple log instance
|
static java.lang.String |
SHOW_DATE_TIME_KEY |
static java.lang.String |
SHOW_LOG_NAME_KEY |
static java.lang.String |
SHOW_SHORT_LOG_NAME_KEY |
static java.lang.String |
SHOW_THREAD_NAME_KEY |
private static long |
START_TIME |
static java.lang.String |
SYSTEM_PREFIX
All system properties used by
SimpleLogger start with this
prefix |
static java.lang.String |
WARN_LEVEL_STRING_KEY |
name
ROOT_LOGGER_NAME
Constructor and Description |
---|
SimpleLogger(java.lang.String name)
Package access allows only
SimpleLoggerFactory to instantiate
SimpleLogger instances. |
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
computeShortName() |
void |
debug(java.lang.String msg)
A simple implementation which logs messages of level DEBUG according to
the format outlined above.
|
void |
debug(java.lang.String format,
java.lang.Object... argArray)
Perform double parameter substitution before logging the message of level
DEBUG according to the format outlined above.
|
void |
debug(java.lang.String format,
java.lang.Object param1)
Perform single parameter substitution before logging the message of level
DEBUG according to the format outlined above.
|
void |
debug(java.lang.String format,
java.lang.Object param1,
java.lang.Object param2)
Perform double parameter substitution before logging the message of level
DEBUG according to the format outlined above.
|
void |
debug(java.lang.String msg,
java.lang.Throwable t)
Log a message of level DEBUG, including an exception.
|
void |
error(java.lang.String msg)
A simple implementation which always logs messages of level ERROR
according to the format outlined above.
|
void |
error(java.lang.String format,
java.lang.Object... argArray)
Perform double parameter substitution before logging the message of level
ERROR according to the format outlined above.
|
void |
error(java.lang.String format,
java.lang.Object arg)
Perform single parameter substitution before logging the message of level
ERROR according to the format outlined above.
|
void |
error(java.lang.String format,
java.lang.Object arg1,
java.lang.Object arg2)
Perform double parameter substitution before logging the message of level
ERROR according to the format outlined above.
|
void |
error(java.lang.String msg,
java.lang.Throwable t)
Log a message of level ERROR, including an exception.
|
private void |
formatAndLog(int level,
java.lang.String format,
java.lang.Object... arguments)
For formatted messages, first substitute arguments and then log.
|
private void |
formatAndLog(int level,
java.lang.String format,
java.lang.Object arg1,
java.lang.Object arg2)
For formatted messages, first substitute arguments and then log.
|
private java.lang.String |
getFormattedDate() |
void |
info(java.lang.String msg)
A simple implementation which logs messages of level INFO according to
the format outlined above.
|
void |
info(java.lang.String format,
java.lang.Object... argArray)
Perform double parameter substitution before logging the message of level
INFO according to the format outlined above.
|
void |
info(java.lang.String format,
java.lang.Object arg)
Perform single parameter substitution before logging the message of level
INFO according to the format outlined above.
|
void |
info(java.lang.String format,
java.lang.Object arg1,
java.lang.Object arg2)
Perform double parameter substitution before logging the message of level
INFO according to the format outlined above.
|
void |
info(java.lang.String msg,
java.lang.Throwable t)
Log a message of level INFO, including an exception.
|
(package private) static void |
init() |
boolean |
isDebugEnabled()
Are
debug messages currently enabled? |
boolean |
isErrorEnabled()
Are
error messages currently enabled? |
boolean |
isInfoEnabled()
Are
info messages currently enabled? |
protected boolean |
isLevelEnabled(int logLevel)
Is the given log level currently enabled?
|
boolean |
isTraceEnabled()
Are
trace messages currently enabled? |
boolean |
isWarnEnabled()
Are
warn messages currently enabled? |
(package private) static void |
lazyInit() |
private void |
log(int level,
java.lang.String message,
java.lang.Throwable t)
This is our internal implementation for logging regular
(non-parameterized) log messages.
|
void |
log(LoggingEvent event) |
(package private) java.lang.String |
recursivelyComputeLevelString() |
protected java.lang.String |
renderLevel(int level) |
void |
trace(java.lang.String msg)
A simple implementation which logs messages of level TRACE according to
the format outlined above.
|
void |
trace(java.lang.String format,
java.lang.Object... argArray)
Perform double parameter substitution before logging the message of level
TRACE according to the format outlined above.
|
void |
trace(java.lang.String format,
java.lang.Object param1)
Perform single parameter substitution before logging the message of level
TRACE according to the format outlined above.
|
void |
trace(java.lang.String format,
java.lang.Object param1,
java.lang.Object param2)
Perform double parameter substitution before logging the message of level
TRACE according to the format outlined above.
|
void |
trace(java.lang.String msg,
java.lang.Throwable t)
Log a message of level TRACE, including an exception.
|
void |
warn(java.lang.String msg)
A simple implementation which always logs messages of level WARN
according to the format outlined above.
|
void |
warn(java.lang.String format,
java.lang.Object... argArray)
Perform double parameter substitution before logging the message of level
WARN according to the format outlined above.
|
void |
warn(java.lang.String format,
java.lang.Object arg)
Perform single parameter substitution before logging the message of level
WARN according to the format outlined above.
|
void |
warn(java.lang.String format,
java.lang.Object arg1,
java.lang.Object arg2)
Perform double parameter substitution before logging the message of level
WARN according to the format outlined above.
|
void |
warn(java.lang.String msg,
java.lang.Throwable t)
Log a message of level WARN, including an exception.
|
(package private) void |
write(java.lang.StringBuilder buf,
java.lang.Throwable t) |
protected void |
writeThrowable(java.lang.Throwable t,
java.io.PrintStream targetStream) |
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, warn
getName, readResolve
private static final long serialVersionUID
private static long START_TIME
protected static final int LOG_LEVEL_TRACE
protected static final int LOG_LEVEL_DEBUG
protected static final int LOG_LEVEL_INFO
protected static final int LOG_LEVEL_WARN
protected static final int LOG_LEVEL_ERROR
protected static final int LOG_LEVEL_OFF
private static boolean INITIALIZED
static SimpleLoggerConfiguration CONFIG_PARAMS
protected int currentLogLevel
private transient java.lang.String shortLogName
public static final java.lang.String SYSTEM_PREFIX
SimpleLogger
start with this
prefixpublic static final java.lang.String LOG_KEY_PREFIX
public static final java.lang.String CACHE_OUTPUT_STREAM_STRING_KEY
public static final java.lang.String WARN_LEVEL_STRING_KEY
public static final java.lang.String LEVEL_IN_BRACKETS_KEY
public static final java.lang.String LOG_FILE_KEY
public static final java.lang.String SHOW_SHORT_LOG_NAME_KEY
public static final java.lang.String SHOW_LOG_NAME_KEY
public static final java.lang.String SHOW_THREAD_NAME_KEY
public static final java.lang.String DATE_TIME_FORMAT_KEY
public static final java.lang.String SHOW_DATE_TIME_KEY
public static final java.lang.String DEFAULT_LOG_LEVEL_KEY
SimpleLogger(java.lang.String name)
SimpleLoggerFactory
to instantiate
SimpleLogger instances.static void lazyInit()
static void init()
java.lang.String recursivelyComputeLevelString()
private void log(int level, java.lang.String message, java.lang.Throwable t)
level
- One of the LOG_LEVEL_XXX constants defining the log levelmessage
- The message itselft
- The exception whose stack trace should be loggedprotected java.lang.String renderLevel(int level)
void write(java.lang.StringBuilder buf, java.lang.Throwable t)
protected void writeThrowable(java.lang.Throwable t, java.io.PrintStream targetStream)
private java.lang.String getFormattedDate()
private java.lang.String computeShortName()
private void formatAndLog(int level, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)
level
- format
- arg1
- arg2
- private void formatAndLog(int level, java.lang.String format, java.lang.Object... arguments)
level
- format
- arguments
- a list of 3 ore more argumentsprotected boolean isLevelEnabled(int logLevel)
logLevel
- is this level enabled?public boolean isTraceEnabled()
trace
messages currently enabled?public void trace(java.lang.String msg)
msg
- the message string to be loggedpublic void trace(java.lang.String format, java.lang.Object param1)
format
- the format stringparam1
- the argumentpublic void trace(java.lang.String format, java.lang.Object param1, java.lang.Object param2)
format
- the format stringparam1
- the first argumentparam2
- the second argumentpublic void trace(java.lang.String format, java.lang.Object... argArray)
format
- the format stringargArray
- a list of 3 or more argumentspublic void trace(java.lang.String msg, java.lang.Throwable t)
msg
- the message accompanying the exceptiont
- the exception (throwable) to logpublic boolean isDebugEnabled()
debug
messages currently enabled?public void debug(java.lang.String msg)
msg
- the message string to be loggedpublic void debug(java.lang.String format, java.lang.Object param1)
format
- the format stringparam1
- the argumentpublic void debug(java.lang.String format, java.lang.Object param1, java.lang.Object param2)
format
- the format stringparam1
- the first argumentparam2
- the second argumentpublic void debug(java.lang.String format, java.lang.Object... argArray)
format
- the format stringargArray
- a list of 3 or more argumentspublic void debug(java.lang.String msg, java.lang.Throwable t)
msg
- the message accompanying the exceptiont
- the exception (throwable) to logpublic boolean isInfoEnabled()
info
messages currently enabled?public void info(java.lang.String msg)
msg
- the message string to be loggedpublic void info(java.lang.String format, java.lang.Object arg)
format
- the format stringarg
- the argumentpublic void info(java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)
format
- the format stringarg1
- the first argumentarg2
- the second argumentpublic void info(java.lang.String format, java.lang.Object... argArray)
format
- the format stringargArray
- a list of 3 or more argumentspublic void info(java.lang.String msg, java.lang.Throwable t)
msg
- the message accompanying the exceptiont
- the exception (throwable) to logpublic boolean isWarnEnabled()
warn
messages currently enabled?public void warn(java.lang.String msg)
msg
- the message string to be loggedpublic void warn(java.lang.String format, java.lang.Object arg)
format
- the format stringarg
- the argumentpublic void warn(java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)
format
- the format stringarg1
- the first argumentarg2
- the second argumentpublic void warn(java.lang.String format, java.lang.Object... argArray)
format
- the format stringargArray
- a list of 3 or more argumentspublic void warn(java.lang.String msg, java.lang.Throwable t)
msg
- the message accompanying the exceptiont
- the exception (throwable) to logpublic boolean isErrorEnabled()
error
messages currently enabled?public void error(java.lang.String msg)
msg
- the message string to be loggedpublic void error(java.lang.String format, java.lang.Object arg)
format
- the format stringarg
- the argumentpublic void error(java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)
format
- the format stringarg1
- the first argumentarg2
- the second argumentpublic void error(java.lang.String format, java.lang.Object... argArray)
format
- the format stringargArray
- a list of 3 or more argumentspublic void error(java.lang.String msg, java.lang.Throwable t)
msg
- the message accompanying the exceptiont
- the exception (throwable) to logpublic void log(LoggingEvent event)