Dark theme | Light theme

April 9, 2009

Simple Logging Facade for Java (SLF4J) has nice template-like methods

The Simple Logging Facade for Java (SLF4J) library makes logging in Java code easy. We can use the facade and at deployment time decide to log using Log4j, java.util.logging or another framework. The library has a very nice feature called parameterized logging. Using parameterized logging we can increase logging performance for disabled logging statements.

This is best explained with an example:

String entry = "sample string";
if (logger.isDebugEnabled()) {
    logger.debug("We need to add logger.isDebugEnabled(), otherwise our logged " 
    + entry + " is contructed "
    + "by concatenating the String, even if debug logging is disabled.");

logger.debug("By using parameterized logging we can print entry:{} but we don't have the overhead of String concatenation if debug logging is disabled.", entry);