2. Why need logging?
• Format of java logs
• Format of messages
• what to log in java
• which logging level to use for which kind
of messages
3. Why SLF4J?
• Jakarta Commons Logging (JCL)
• java.util.logging.
-Same problem domain
-Well-established library
So why do we re-invent the wheel?
Because details of implementation
matter.
A simpler implementation is preferable to more complex variant even if
the latter offers other advantages.
4. What is SLF4J
Sl4j-Simple Logging Façade for Java
slf4j is a facade, not an implementation.
• Log4J was initially created by Ceki Gülcü. After Log4J Ceki created
SLF4J, so I guess he put everything he learned from Log4J into
SLF4J.
• SLF4J provides a nice way of creating short and easy to read
logging statements in the code, that have only minor resource
consumption if logging is turned off.
• String message = "Could not find our DataSource in
WAT Database.";
logger .error("*** " , message);
logger.debug("{}", complexObject);
logger.debug("Info : x = {}, y = {}, str = {}", new Object[]{info.getX(),
info.getY(), infos.getStr()});
9. How to setup SLF4J and
LOGBack in a web app - fast
• import the following libraries to your WEB-
INF/lib folder:
• WEB-INF
• lib
• logback-classic.x.x.x.jar
• logback-core.x.x.x.jar
• slf4j-api-x.x.x.jar
TOMCAT ( {catlina-home}/lib
logback-classic.1.0.9.jar
logback-core.1.0.9.jar
slf4j-api-1.0.9.jar
10. SLF4J explained..
• Object entry = new SomeObject();
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " +
String.valueOf(entry[i]));
}
logger.debug("The entry is {}.", entry);
13. Marker????? Marker API
• The Marker allow essentially to associate tags
to logs.
• This tags enable the different appenders to
take only some logs.
• Lets imagine an appender who write the log
using encryption and that must only be used
on logs marked as confidentials. The Marker
enable us to implement that.
15. Marker API
• Marker confidentialMarker =
MarkerFactory.getMarker("CONFIDENTIL");
• logger.error(confidentialMarker, “ very vconfidentiel !");
16. MDC
• A simple map (key-value set) maintained by
the logging framework.
• In that map, the application can put some
key-value couple that could be used to add
some informations in the logs.
18. Conclusion
• It‟s really powerful, but remains really simple
to use and the style is the same as the other
existing logging systems.
Simplicity is powerful.
-- Evan Williams
In Java, any string parameter passed to a method is constructed before the method is called. While it make take 2 nanoseconds to evaluate that a logger is disabled for the DEBUG level when logger.debug() is invoked, it may take on “average” over 100 nanosecond to build the string “Hello”+ name. Thus, if you are concerned about performance, you might want to use conditional logging, or the much more convenient new style called parameterized logging introduced in SLF4J.