log4j properties file example

Logging is essential part of programming. It helps developer to track code workflow and fix bugs efficiently. If we get any issue in the code, we check logs for that workflow or functionality.

Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at run time without using

There are two ways, you can do logging using log4j.

  1. log4j.properties
  2. log4j.xml

In this post, we will see how to configure using log4.properties

1) Create simple maven java project.

2) Put log4j entry in pom.xml .

3) Update maven project to download require jar

Right click on project-> Maven -> update project

4) Create log4j.properties file

This file is main file for log4j configuration. It has information about log levels, log appenders.

Log to console:

If you want to log to console, you need to have following lines in log4j.properties.

Log to file :
If you want to log to console, you need to have following lines in log4j.properties and also you need to provide log file name too.

 
Log to console and file both :

If you want to log on both to console and file, you can use below code:

5) Sample class for Logger

Create a class called “LoggingMain.java” in src/main/java in package org.arpit.java2blog

6) Run program:

When you run above program , you will get below output to console and file(applicationLogs.log)

Bingo!! we have successfully configured log4j using properties file in maven project.

Add Comment