Configure log4j using properties file in Java

Log4j is a logging framework based on Java, It is an open source logging framework designed to manage application logs in a flexible and efficient manner.

Configuring Log4j with Properties File:-

One of the key features of Log4j is its easy configuration. To set up Log4j using a properties file, developers can rely on the PropertyConfigurator class. This class facilitates external configuration of Log4j by loading settings from a designated file. The method configure(String configFilename) within the PropertyConfigurator class is used in this process.

Properties vs. XML Configuration

Log4j supports both properties and XML configurations. This tutorial will focus on configuring Log4j through a properties file. To implement Log4j in your project, it’s required to include the log4j.jar file, which can be downloadable from Apache’s official sources.

Configuring Log4j with Properties File:-
  1. Download the log4j.jar File: Begin by acquiring the log4j.jar file from Apache’s website. This file contains the necessary libraries to integrate Log4j into your project.

  2. Create a Properties File: Design a properties file (e.g., to specify the desired logging configuration. This file will contain details about log levels, appenders, layouts, and more.

  3. Specify Logging Settings: Within the properties file, define various logging settings. For instance, set log levels (DEBUG, INFO, WARN, ERROR) for different components of your application.

  4. Configure Appenders: Configure appenders to determine where log messages will be directed. Choose from options like ConsoleAppender (for console output) or FileAppender (for file output).

  5. Define Layouts: Customize the format of log entries using layouts like PatternLayout or HTMLLayout.

  6. Load Configuration: In your Java code, use the PropertyConfigurator class to load the configuration from the properties file using the configure(String configFilename) method.

Project structure:-
    log4j using properties file
    Demo to Configure Log4j using Properties File in java:-
    package com.javaartifacts;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    public class LoggerTest {
        public static Logger logger = Logger.getLogger(LoggerTest.class);
        public static void main(String[] args){
  "configure log4j using properties file in java");


    log4j.rootLogger=DEBUG, consoleAppend, fileAppend log4j.appender.consoleAppend=org.apache.log4j.ConsoleAppender log4j.appender.consoleAppend.Target=System.out log4j.appender.consoleAppend.layout=org.apache.log4j.PatternLayout log4j.appender.consoleAppend.layout.ConversionPattern=%t %-5p %c{2} - %m%n log4j.appender.fileAppend=org.apache.log4j.RollingFileAppender log4j.appender.fileAppend.File=loggerdemo.log log4j.appender.fileAppend.layout=org.apache.log4j.PatternLayout log4j.appender.fileAppend.layout.conversionPattern=%d{MM-dd HH:mm:ss.S} %t %m%n


    main INFO javaartifacts.LoggerTest - configure log4j using properties file in java


    Above file is configured to log on the console as well as to the file named loggerdemo.log. Now if You want to create the log file on daily basis, then You can change the fileAppend configuration with below configuration.

    log4j.appender.fileAppend=org.apache.log4j.DailyRollingFileAppender log4j.appender.fileAppend.File=loggerdemo.log log4j.appender.fileAppend.layout=org.apache.log4j.PatternLayout log4j.appender.fileAppend.layout.conversionPattern=%d{MM-dd HH:mm:ss.S} %t %m%n log4j.appender.P.DatePattern='.'yyy-MM-dd


    Leave a Comment