Log4j Configuration Using Properties File

In this article, you will see how to generate logs using a properties file configured externally.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


Three main components you need to configure :

  • logger : It is used to define level of the root logger and to attach appender.
  • appender : It is used to specify the output destination like console or a file.
  • layout : It is used to specify the format in which the log messages should be logged.

1. Properties file

The following configuration creates the same result as the BasicConfigurator.configure() method in previous article.


log4j.rootLogger=DEBUG, CA


log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

2. Log4j in Java Program

The following Java class is a very simple example that initializes, and then uses, the Log4J logging library for Java applications.

package com.kruders;

import org.apache.log4j.Logger;

public class HelloWorld {
	static final Logger logger = Logger.getLogger(HelloWorld.class);
	public static void main(String[] args) {
		logger.info("Hello World!!!");

When you run the above example you’ll get an output like:

0 [main] INFO com.kruders.HelloWorld – Hello World!!!

You can download the source code of this example here.


No comments yet.

Leave a Reply