Log4j Maven configuration 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

Lets integrate Log4j in simple maven project.


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) Sample class for Logger 

Create a class called "LoggingMain.java" in src/main/java in package org.arpit.java2blog
package org.arpit.java2blog;

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

public class LoggingMain {

  private static final Logger logger = LogManager.getLogger(LoggingMain.class);
 public static void main(String[] args) {
  // basic log4j configurator
  logger.info("Hello world");
  logger.info("we are in logger info mode");



5) Run program:

When you run above program , you will get below output:
0 [main] INFO org.arpit.java2blog.LoggingMain  - Hellow world
1 [main] INFO org.arpit.java2blog.LoggingMain  - we are in logger info mode
Bingo!! we have successfully configured log4j in maven project.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012