Last active
August 29, 2015 14:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To know more about logback appenders, pattern layout and filters see the manual http://logback.qos.ch/manual/introduction.html