Log4j configuration using properties file example

In this tutorial you will learn how you can use the properties file to configure log4j framework. Actually log4j is usually configured using a properties file or xml file externally; you have to provide the external properties or xml file for your java application.

Once the log statements are place in java code you can easily control using properties file without modifying the java source code.

There are three main log4j components that you need to configure to get the result and those are logger, appender and layout. We already have discussed about the logger, appender and layout.

In last example we have used the BasicConfigurator.configure () method by default it uses the ConsoleAppender and PatternLayout for all the loggers.

Below configuration file creates the same result as the BasicConfigurator.configure () method.

log4j.rootLogger=DEBUG, CA 
  
log4j.appender.CA=org.apache.log4j.ConsoleAppender 
  
log4j.appender.CA.layout=org.apache.log4j.PatternLayout 
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

The log4j.rootLogger is the one that resides on the top of the logger hierarchy. Here we set its level to DEBUG and added the console appender (CA) to it. The console appender can have arbitrary name, here its name is CA.

You need to create an appender as shown and set its layout to PatternLayout. The PatternLayout uses the ConversionPattern to format the message.

Once the appender is created and its layout is set you need to specify which loggers can use this appender. If you set this appender to the rootLogger then all the loggers will log message to this appender. The rootLogger is on top of the hierarchy all the loggers will inherit its logger level and its appenders. You will see this in more detail in the coming examples.

Below is example of code which uses the configuration from properties file for log4j.

package com.java.connect.log4j;
 
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 
public class Log4jPropertyConfiguratorExample {
 
    private static Logger LOGGER = Logger.getLogger(Log4jPropertyConfiguratorExample.class);
 
    public static void main(String[] args) {
        // Use the PropertyConfigurator to load the log4g configuration from properites file
        PropertyConfigurator.configure("log4j1.properties");
        LOGGER.debug("Debug -- Hello World");
        LOGGER.info("Info -- Hello World");
        LOGGER.error("Error -- Hello World");
        LOGGER.fatal("Fatal -- Hello World");
        LOGGER.warn("Warn -- Hello World");
    }
}

Below is the output of code as:

0    [main] DEBUG com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Debug -- Hello World
2    [main] INFO  com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Info -- Hello World
3    [main] ERROR com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Error -- Hello World
3    [main] FATAL com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Fatal -- Hello World
3    [main] WARN  com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Warn -- Hello World

Now in properties file if you change the log4j.rootLogger=DEBUG, CA as log4j.rootLogger=ERROR, CA. The output of the above programme will be as:

0    [main] ERROR com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Error -- Hello World
4    [main] FATAL com.java.connect.log4j.Log4jPropertyConfiguratorExample  - Fatal -- Hello World

Because you have changed logger level as ERROR, So only till ERROR level logs will display other will be elapsed.