Java Reference
In-Depth Information
There are various methods used to log information at different levels of severity, which are
shown in Table 16-1 .
Table 16-1. SLF4j logging methods
Name Meaning
trace
Verbose debugging (disabled by default)
debug Verbose debugging
info
low-level informational message
warn
Possible error
error
Serious error
One of the advantages of SLF4j over most of the other logging APIs is the avoidance of the
“dead string” anti-pattern. In many other logger APIs you may find code like:
logger . log ( "The value is " + object + "; this is not good" );
This can lead to a performance problem, in that the object's toString() is implicitly called,
and two string concatenations performed, before we even know if the logger is going to use
them! If this is in code that is called repeatedly, a lot of overhead can be wasted.
This led the other logging packages to offer “code guards,” based on logger methods that can
find out very quickly if a logger is enabled, leading to code like the following:
iif ( logger . isEnabled ()) {
logger . log ( "The value is " + object + "; this is not good" );
}
This solves the performance problem, but clutters the code! SLF4J's solution is to use a
mechanism similar to (but not quite compatible with) Java's MessageFormat mechanism
(see Formatting Messages with MessageFormat ), as shown in Example 16-11 .
Example 16-11. logging/Slf4jDemo2.java
Search WWH ::




Custom Search