November 2, 2015

Example on Log4j2 with RollingFile

pom.xml file

<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>log4j2-example</groupId>
 <artifactId>log4j2-example</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>
  <!-- Log4J 2 : required -->
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.3</version>
  </dependency>
 </dependencies>
</project>

log4j2.xml file

Note: the log4j2.xml file is located under PORJECT_HOME\src\main\resources\log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
 <Properties>
  <Property name="log-path">logs</Property>
 </Properties>
 <Appenders>
  <Console name="console-log" target="SYSTEM_OUT">
   <PatternLayout pattern="[%-5level][%t] %c{1}:%L- %msg%n" />
  </Console>
  <RollingFile name="trace-log" fileName="${log-path}/myexample.log"
   filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log">
   <PatternLayout>
    <pattern>[%-5level] %d [%t] %c{1}:%L- %msg%n</pattern>
   </PatternLayout>
   <Policies>
    <SizeBasedTriggeringPolicy size="1 KB" />
   </Policies>
   <DefaultRolloverStrategy max="4" />
  </RollingFile>
  <RollingFile name="error-log" fileName="${log-path}/myexample-error.log"
   filePattern="${log-path}/ics-print-info-error-%d{yyyy-MM-dd}.log">
   <PatternLayout>
    <!-- %-5level %d [%t] %c:%M(%L): %m%n -->
    <pattern>[%-5level] %d [%t] %c:%M:%L- %m%n</pattern>
   </PatternLayout>
   <Policies>
    <SizeBasedTriggeringPolicy size="1 KB" />
   </Policies>
   <DefaultRolloverStrategy max="4" />
  </RollingFile>
 </Appenders>
 <Loggers>
  <Logger name="sawalha" level="debug" additivity="false"
   includeLocation="true">
   <appender-ref ref="trace-log" level="debug" />
   <appender-ref ref="error-log" level="error" />
   <appender-ref ref="console-log" level="info" />
  </Logger>
  <Root level="debug" additivity="false">
   <AppenderRef ref="console-log" />
  </Root>
 </Loggers>
</Configuration>

Log4j2Example class file

package sawalha.log4j2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4j2Example {
    static Logger log = LogManager.getLogger(Log4j2Example.class.getName());

    public static void main(String[] args) throws IOException {
        System.out.println("===> Please enter a number:\n===>");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int number = Integer.valueOf(br.readLine());
        log.info("Info : number is " + number);
        log.warn("Warning : number is " + number);
        log.debug("Debug : number is " + number);
        log.error("Error : number is " + number);
        log.fatal("Fatal : number is " + number);

        if (number > 100) {
            log.info("Info : You chose a number > 100 ");
            log.warn("Warning : You chose a number > 100 ");
            log.debug("Debug : You chose a number > 100 ");
            log.error("Error : You chose a number > 100 ");
            log.fatal("Fatal : You chose a number > 100 ");

        } else {
            log.info("Info : You chose a number < 100 ");
            log.warn("Warning : You chose a number < 100 ");
            log.debug("Debug : You chose a number < 100 ");
            log.error("Error : You chose a number < 100 ");
            log.fatal("Fatal : You chose a number < 100 ");
        }
        String numberStr = String.valueOf(number);
        for (int i = 0; i <= 10; i++) {
            if (numberStr.contains(String.valueOf(i))) {
                log.info("Info : Your number has the digit " + i);
                log.warn("Warning : Your number has the digit " + i);
                log.debug("Debug : Your number has the digit " + i);
                log.error("Error : Your number has the digit " + i);
                log.fatal("Fatal : Your number has the digit " + i);
            }
        }
    }
}

No comments: