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.

Steps:

1) Create simple maven java project.

2) Put log4j entry in pom.xml .


 log4j
 log4j
 1.2.17

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
  BasicConfigurator.configure();
  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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.