Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Logging framework - SLF4J (Simple Logging Facade for Java)

Maven dependency (version managed by our maven parent project pom.xml)

Code Block
languagexml
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
</dependency>	

How to use

Code Block
languagejava
// import required classes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
// define logger
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationServiceImpl.class);
 
// use in code
try {
// logic
} catch (final StorageException e) {
	final String message = "(" + accountId + ") fail.";
	LOGGER.error(message, e);
	throw new AuthenticationServiceException(message, e);
}

FATAL logging level (marker usage example)

Code Block
languagejava
// import utility
import net.bolbat.utils.logging.LoggingUtils;
 
// use in code
try {
// logic
} catch (final ManagerException e) {
	final String message = "Can't initialize persistence service.";
	LOGGER.error(LoggingUtils.FATAL, message, e);
	throw new ServiceInstantiationException(message, e);
}

SLF4J API implementations

  • default no-operation (NOP) logger, included in slf4j-api
  • logback (version managed by our maven parent project pom.xml)

    Code Block
    languagexml
     <dependency>
    	<groupId>ch.qos.logback</groupId>
    	<artifactId>logback-classic</artifactId>
    </dependency>
  • apache log4j (version managed by our maven parent project pom.xml)

    Code Block
    languagexml
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-log4j12</artifactId>
    </dependency>
  • apache commons-logging (version managed by our maven parent project pom.xml

    Code Block
    languagexml
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-jcl</artifactId>
    </dependency>
  • java util logging (version managed by our maven parent project pom.xml

    Code Block
    languagexml
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-jdk14</artifactId>
    </dependency>

How to use for JUnit's without real dependency to logger implementation (logback example)

  • Add maven dependency only for testing scope (version managed by our maven parent project pom.xml

    Code Block
    languagexml
    <dependency>
    	<groupId>ch.qos.logback</groupId>
    	<artifactId>logback-classic</artifactId>
    	<scope>test</scope>
    </dependency>
  • Configure logger (example configuration)

Logging redirection from other loggers to SLF4J (just add required dependency)

  • apache log4j 

    Code Block
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.5</version>
    </dependency>
  • apache commons-logging 

    Code Block
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.5</version>
    </dependency>
  • java util logging 

    Code Block
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>1.7.5</version>
    </dependency>
  • details can be found on http://www.slf4j.org/legacy.html