December 14, 2015

Example on SLF4J with RollingFile

pom.xml

<!-- slf4j Logger -->
<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
</dependency>
<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
</dependency>
<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
</dependency>

logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

 <!-- Send debug messages to System.out -->
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
  <encoder>
   <Pattern>
    [%-5level] %logger:%line - %message%n
   </Pattern>
  </encoder>
 </appender>

 <!-- Send debug messages to a file at "/logs/test.log" -->
 <appender name="FILE"
  class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>logs/test.log</file>
  <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
   <Pattern>
    [%-5level] %d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %logger{36}:%line - %msg%n
   </Pattern>
  </encoder>

  <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
   <FileNamePattern>logs/test.%i.log.zip</FileNamePattern>
   <MinIndex>1</MinIndex>
   <MaxIndex>10</MaxIndex>
  </rollingPolicy>

  <triggeringPolicy
   class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
   <MaxFileSize>2MB</MaxFileSize>
  </triggeringPolicy>
 </appender>

 <logger name="com.javacodegeeks.examples.logbackexample.beans"
  level="INFO" additivity="false">
  <appender-ref ref="STDOUT" />
 </logger>

 <!-- By default, the level of the root level is set to DEBUG -->
 <root level="TRACE">
  <appender-ref ref="STDOUT" />
  <appender-ref ref="FILE" />
 </root>
</configuration>

Test Class

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class TestLog {

    final static Logger log = LoggerFactory.getLogger(TestLog.class.getName());

    public static void main(String[] args) {

        log.info("info");
        log.error("error");
        log.warn("warning");
        log.debug("debug");
        log.trace("trace");

        log.info("info {}", "one param");
        log.error("error {}", "one param");
        log.warn("warning {}", "one param");
        log.debug("debug {}", "one param");
        log.trace("trace {}", "one param");

        log.info("info {} and {}", "param-1", "param-2");
        log.error("error {} and {}", "param-1", "param-2");
        log.warn("warning {} and {}", "param-1", "param-2");
        log.debug("debug {} and {}", "param-1", "param-2");
        log.trace("trace {} and {}", "param-1", "param-2");

        log.error("Custome Exception ", new Exception("cusotome error msg"));
        log.error("ERROR in {} with {}", "className", "inParam", new Exception("custome error msg"));

        String s = "Hello";
        try {
            Integer.valueOf(s);
        } catch (NumberFormatException e) {
            log.error("Failed to format {}", s, e);
        }

        Marker fatal = MarkerFactory.getMarker("FATAL");
        Logger logger = LoggerFactory.getLogger("aLogger");
        try {
            Integer i = Integer.valueOf(s);
        } catch (NumberFormatException e) {
            logger.error(fatal, "Fatal Error", e);
        }

        logger.debug("Set {1,2} differs from {}", "3");
        logger.debug("Set {1,2} differs from {{}}", "3");
        logger.debug("Set \\{} differs from {}", "3");
        logger.debug("File name is C:\\\\{}.", "file.zip");
    }

}