Log4j multiple files example

In this example, I show how to configure log4j to write logs to multiple files based on the category. To do this, we would be using LevelRangeFilter.

The objective is to show:

  • all the logs in console.
  • logs from com.idodevjobs.log4jexample.main package and its sub-packages based on category in info.log and error.log files. i.e.
    • debug to info in info.log
    • warn to fatal in error.log

src\log4j.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
    </appender>

    <appender name="info-log" class="org.apache.log4j.FileAppender">
        <param name="File" value="info.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="debug" />
            <param name="LevelMax" value="info" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <appender name="error-log" class="org.apache.log4j.FileAppender">
        <param name="Append" value="false" />
        <param name="File" value="error.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="warn" />
            <param name="LevelMax" value="fatal" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <logger name="com.idodevjobs.log4jexample.main">
        <appender-ref ref="info-log" />
        <appender-ref ref="error-log" />
    </logger>

    <root>
        <level value="debug" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

src\com\idodevjobs\log4jexample\main\Log4jExample.java


package com.idodevjobs.log4jexample.main;

import com.idodevjobs.util.Util;
import org.apache.log4j.Logger;

public class Log4jExample {
    private static Logger logger = Logger.getLogger(Log4jExample.class);

    public static void main(String[] args) {
        logger.debug("log4jexample:a sample debug message");
        logger.info("log4jexample:a sample info  message");
        logger.warn("log4jexample:a sample warn  message");
        logger.error("log4jexample:a sample error message");
        logger.fatal("log4jexample:a sample fatal message");

        Util util = new Util();
        util.aMethod();
    }
}

src\com\idodevjobs\util\Util.java


package com.idodevjobs.util;

import org.apache.log4j.Logger;

public class Util {
    private static Logger logger = Logger.getLogger(Util.class);

    public void aMethod() {
        logger.debug("util:a sample debug message");
        logger.info ("util:a sample info  message");
        logger.warn ("util:a sample warn  message");
        logger.error("util:a sample error message");
        logger.fatal("util:a sample fatal message");
    }
}

Output: Console


DEBUG Log4jExample - log4jexample:a sample debug message
INFO  Log4jExample - log4jexample:a sample info  message
WARN  Log4jExample - log4jexample:a sample warn  message
ERROR Log4jExample - log4jexample:a sample error message
FATAL Log4jExample - log4jexample:a sample fatal message
DEBUG Util - util:a sample debug message
INFO  Util - util:a sample info  message
WARN  Util - util:a sample warn  message
ERROR Util - util:a sample error message
FATAL Util - util:a sample fatal message

Output: info.log


DEBUG Log4jExample - log4jexample:a sample debug message
INFO  Log4jExample - log4jexample:a sample info  message

Output: error.log


WARN  Log4jExample - log4jexample:a sample warn  message
ERROR Log4jExample - log4jexample:a sample error message
FATAL Log4jExample - log4jexample:a sample fatal message

.. HTH ..

Advertisements
This entry was posted in java and tagged , . Bookmark the permalink.

3 Responses to Log4j multiple files example

  1. Anonymous says:

    where are info.log and error.log files located

    Like

  2. Pratyush says:

    where are info.log and error.log files located

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s