04 - log4j Configurations

4.1 Overview

In previous chapters we discussed about log4j and its components. In this chapter we will discuss how to configure the log4j and different configuration approaches.

4.2 Configurations

When we say log4j configuration, we mean configuring its components like layouts, appenders, levels etc.

Configuration can be done in code or in a properties or xml file. Using a configuration file approach has its advantages as it is not tied to code and we can change the configurations without changing any code or binaries.

4.2.1 Basic Configurator

The simplest way to configure the log4j is using BasicConfigurator class in which we just need to call configure() method of BasicConfigurator class and it will direct all the log statements to console.

Lets look at the below code example in which we have used the BasicConfigurator and logged different log messages. 

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

publicclassBasicConf {

    static Logger logger = Logger.getLogger(BasicConf.class);

    publicstaticvoid main(String[] args) {

        BasicConfigurator.configure();
        logger.error(" ERROR Log Message!!! ");
        logger.info(" INFO Log Message!!! ");
        logger.debug(" DEBUG Log Message!!! ");
        logger.fatal(" FATAL Log Message!!! ");
    }
}

Above program outputs the log statement on console (refer below-)

0 [main] ERROR com.log4j.examples.BasicConf  -  ERROR Log Message!!! 
1 [main] INFO com.log4j.examples.BasicConf  -  INFO Log Message!!! 
1 [main] DEBUG com.log4j.examples.BasicConf  -  DEBUG Log Message!!! 
1 [main] FATAL com.log4j.examples.BasicConf  -  FATAL Log Message!!!

4.2.2 log4j.properties

By default log4j looks for a file named log4j.properties file in classpath to load the configurations.

If you want to load configuration file available at other path or name other than log4j.properties, then you can use  PropertyConfigurator.configure() method and pass in the file name as an argument like below

PropertyConfigurator.configure("custom.properties"); 

Lets have a look at sample log4j.properties to configure Root Logger file which will log the messages to console.

log4j.rootLogger=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n

Below log4j.properties tells how to send log the messages to File. File will be created automatically.

log4j.rootLogger=INFO, file

# Direct log messages to a file
log4j.appender.file=org.apache.log4j.RollingFileAppender

# log file path with name
log4j.appender.file.File=C:\\logigng.log

# max size of log file, after this a new file will be created
log4j.appender.file.MaxFileSize=10MB

# max number of log files 
log4j.appender.file.MaxBackupIndex=10

# layouts
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n

Below log4j.properties tells how to send log the messages to both Console and File. 

log4j.rootLogger=INFO, console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to a file
log4j.appender.file=org.apache.log4j.RollingFileAppender

# log file path with name
log4j.appender.file.File=D:\\logs.log

# max size of log file, after this a new file will be created
log4j.appender.file.MaxFileSize=10MB

# max number of log files 
log4j.appender.file.MaxBackupIndex=10

# layouts
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n  

4.2.3 log4j.xml

Instead of log4j.properties, we can use xml file of name log4j.xml to configure the log4j and place it in classpath.

Log4j framework looks for log4j.xml file first and if not found then it look for log4j.properties file in classpath so we should create the properties file or xml file in src folder.

If you want to load configuration file available at other path or name other than log4j.properties, then you can use  PropertyConfigurator.configure() method and pass in the file name as an argument like below

PropertyConfigurator.configure("custom.xml");

XML approach is recommended as it provides more configurations like configuring filters, special appenders

Lets have a look at below xml configuration for console log.

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

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
        value="%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
    </appender>

    <root>
        <level value="INFO" />
        <appender-ref ref="console" />
    </root>   
</log4j:configuration> 

Below log4j.xml tells how to send log the messages to File. File will be created automatically.

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

    <appender name="file" class="org.apache.log4j.RollingFileAppender">
      <param name="append" value="false" />
      <param name="maxFileSize" value="10KB" />
      <param name="maxBackupIndex" value="5" />

      param name="file" value="D:\\logs.log" />
      <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n" />
      </layout>
    </appender>

    <root>
        <level value="INFO" />
        <appender-ref ref="file" />
    </root>

</log4j:configuration>

Below log4j.properties tells how to send log the messages to both Console and File. 

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

   appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
            value="%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n" />
      </layout>
   </appender>

   <appender name="file" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false" />
    <param name="maxFileSize" value="10MB" />
    <param name="maxBackupIndex" value="10" />
    <param name="file" value="D:\\logs.log" />
        <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" 
           value="%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="INFO" />
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>
</log4j:configuration>

Preference

  • In case both log4j.xml and log4j.properties files are present in classpath, then log4j.xml will be used.
  • In case both log4j.xml and log4j.properties files are present in classpath, and BasicConfigurator us used then also log4j.xml/properties will be used.

Like us on Facebook