Simple log4j configuration in java


The purpose of inserting log statements into the code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.
Features of Log4j

We can enable logging at runtime without modifying the application binary.

We can control the behavior of logging by editing only the configuration file, no need to touch the application binary.

Developer are always clear with detailed context for application failures.

Log4j has one of the distinctive features – the notion of inheritance. Using this logger hierarchy feature we are able to control the log statements output at arbitrarily fine granularity but also at great ease. This helps to reduce the volume of logged output and the cost of logging.

We can set many targets for the log output such as a file, an OutputStream, a, a remote log4j server, a remote Unix Syslog daemon, or many other output targets.

Loggin Levels

FATAL: shows messages at a FATAL level only
ERROR: Shows messages classified as ERROR and FATAL
WARNING: Shows messages classified as WARNING, ERROR, and FATAL
INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL
DEBUG: Shows messages classified as DEBUG, INFO, WARNING, ERROR, and FATAL
ALL – The ALL Level has the lowest possible rank and is intended to turn on all logging.
OFF – The OFF Level has the highest possible rank and is intended to turn off logging.

For more information please go through Apache Log4j

I am giving here simple logginh example using log4j.xml configuration. This will log the messages to the console as well as to file. I have used here RollingFileAppender so that we can have multiple backup files. This log4j.xml file is put into the classpath i.e. under src directory.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j=''>
    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
    <appender name="RFA" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="sample.log" />
        <param name="ImmediateFlush" value="true" />
        <param name="MaxFileSize" value="5KB" />
        <param name="MaxBackupIndex" value="5" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
        <level value="DEBUG" />
        <appender-ref ref="CA" />
        <appender-ref ref="RFA" />


Write a simple java class for testing it. We need log4j.jar file to be put into the classpath.

package in.sblog.log4j.test;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class TestLog {

    private static Logger logger = Logger.getLogger(TestLog.class);

    public static void main(String[] args) {
        logger.debug("log4j configuration is successful.");



Console output:

0    [main] DEBUG in.sblog.log4j.test.TestLog  - log4j configuration is successful.

File Output:

0    [main] DEBUG in.sblog.log4j.test.TestLog  - log4j configuration is successful.


That’s all. Thanks for your time. Please do not forget to leave a comment.

Simple log4j configuration in java

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top