Skip to content

Instantly share code, notes, and snippets.

@arulrajnet
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arulrajnet/11c263e3052074ce7b08 to your computer and use it in GitHub Desktop.
Save arulrajnet/11c263e3052074ce7b08 to your computer and use it in GitHub Desktop.
SLF4J with logback. Configuration, pom and example file to catch your dependencies logs also. Different log file for to catch error only
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Put this file into src/main/resources -->
<configuration>
<contextName>WebContext</contextName>
<jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%p [%t] %c{1}.%M\(%L\) | %m%n</pattern>
</encoder>
</appender>
<!-- This appender to catch all except trace -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logback_example_debug.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logback_example_debug.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<pattern>%d{ISO8601} %-5p [%c{3}] \(%t:%X{}\) %m%n</pattern>
</encoder>
</appender>
<!-- This appender to catch only errors -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logback_example_error.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logback_example_error.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>%d{ISO8601} %-5p [%c{3}] \(%t:%X{}\) %m%n</pattern>
</encoder>
</appender>
<root>
<level value="${logger.root.level}"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="DEBUG_FILE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
<!-- Third Parties Logs -->
<logger name="org.springframework">
<level value="WARN"/>
</logger>
<logger name="com.github.gist">
<level value="DEBUG"/>
</logger>
</configuration>
package com.github.gist;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerExample {
public static void main(String[] args) {
Logger log = LoggerFactory.getLogger(LoggerExample.class);
log.debug("You are in main");
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.arulraj.example</groupId>
<artifactId>logback-example</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- LOG level -->
<logger.root.level>DEBUG</logger.root.level>
<logback.version>1.1.1</logback.version>
<slf4j.version>1.7.5</slf4j.version>
</properties>
<dependencies>
<!-- Logger -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</project>
@arulrajnet
Copy link
Author

To know more about logback appenders, pattern layout and filters see the manual http://logback.qos.ch/manual/introduction.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment