Child pages
  • Logging Configuration and Usage
Skip to end of metadata
Go to start of metadata

Logging framework - SLF4J (Simple Logging Facade for Java)

Maven dependency (by default version managed by our maven parent project)

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
</dependency>	

How to use

// 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)

// use in code
try {
// logic
} catch (final Exception e) {
	final String message = "Can't initialize persistence service.";
	LOGGER.error(MarkerFactory.getMarker("FATAL"), message, e);
	throw new ServiceInstantiationException(message, e);
}

SLF4J API implementations

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

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

    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-log4j12</artifactId>
    </dependency>
  • apache commons-logging (by default version managed by our maven parent project) 

    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-jcl</artifactId>
    </dependency>
  • java util logging (by default version managed by our maven parent project) 

    <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 (by default version managed by our maven parent project) 

    <dependency>
    	<groupId>ch.qos.logback</groupId>
    	<artifactId>logback-classic</artifactId>
    	<scope>test</scope>
    </dependency>
  • Configure logger 

    <configuration>
    	<appender name="DebugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    			<level>DEBUG</level>
    		</filter>
    		<file>logs/msk-control-debug.log</file>
    		<append>true</append>
    		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
    			<fileNamePattern>logs/msk-control-debug.%i.log.zip</fileNamePattern>
    			<minIndex>1</minIndex>
    			<maxIndex>5</maxIndex>
    		</rollingPolicy>
    		<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    			<maxFileSize>100MB</maxFileSize>
    		</triggeringPolicy>
    		<encoder>
    			<pattern>%r %d [%t] %-5le %marker %lo{32}:%L - %msg%n</pattern>
    		</encoder>
    	</appender>
    	<appender name="InfoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    			<level>INFO</level>
    		</filter>
    		<file>logs/msk-control-info.log</file>
    		<append>true</append>
    		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
    			<fileNamePattern>logs/msk-control-info.%i.log.zip</fileNamePattern>
    			<minIndex>1</minIndex>
    			<maxIndex>5</maxIndex>
    		</rollingPolicy>
    		<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    			<maxFileSize>100MB</maxFileSize>
    		</triggeringPolicy>
    		<encoder>
    			<pattern>%r %d [%t] %-5le %marker %lo{32}:%L - %msg%n</pattern>
    		</encoder>
    	</appender>
    	<appender name="WarnAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    			<level>WARN</level>
    		</filter>
    		<file>logs/msk-control-warn.log</file>
    		<append>true</append>
    		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
    			<fileNamePattern>logs/msk-control-warn.%i.log.zip</fileNamePattern>
    			<minIndex>1</minIndex>
    			<maxIndex>5</maxIndex>
    		</rollingPolicy>
    		<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    			<maxFileSize>100MB</maxFileSize>
    		</triggeringPolicy>
    		<encoder>
    			<pattern>%r %d [%t] %-5le %marker %lo{32}:%L - %msg%n</pattern>
    		</encoder>
    	</appender>
    	<appender name="ErrorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    			<level>ERROR</level>
    		</filter>
    		<file>logs/msk-control-error.log</file>
    		<append>true</append>
    		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
    			<fileNamePattern>logs/msk-control-error.%i.log.zip</fileNamePattern>
    			<minIndex>1</minIndex>
    			<maxIndex>5</maxIndex>
    		</rollingPolicy>
    		<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    			<maxFileSize>100MB</maxFileSize>
    		</triggeringPolicy>
    		<encoder>
    			<pattern>%r %d [%t] %-5le %marker %lo{32}:%L - %msg%n</pattern>
    		</encoder>
    	</appender>
    	<appender name="ConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    			<level>INFO</level>
    		</filter>
    		<encoder>
    			<pattern>%r %d [%t] %-5le %marker %lo{32}:%L - %msg%n</pattern>
    		</encoder>
    	</appender>
    
    	<logger name="org.moskito.control" level="DEBUG" additivity="false">
    		<appender-ref ref="DebugAppender" />
    		<appender-ref ref="InfoAppender" />
    		<appender-ref ref="WarnAppender" />
    		<appender-ref ref="ErrorAppender" />
    		<appender-ref ref="ConsoleAppender" />
    	</logger>
    
    	<root level="ERROR">
    		<appender-ref ref="ErrorAppender" />
    		<appender-ref ref="ConsoleAppender" />
    	</root>
    </configuration> 

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

  • apache log4j 

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

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

    <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

 

 

 

 

  • No labels