HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j / Log4j Maven Configuration Example

Log4j Maven Configuration Example

Log4j is a simple and flexible logging framework. Logging equips the developer with detailed context for application failures. With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost.

Below are the most basic steps to configure log4j logging support in your project.

1) Create a maven project

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=Log4jTestProject
 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Run above command in your eclipse workspace or any other IDE you are working in. If you already have a project in your workspace, then directly go to step 3.

2) Convert the project to eclipse supported java project

mvn eclipse:eclipse

Above command will convert maven project to eclipse java project. Now, import the project to eclipse.

3) Update pom.xml file with log4j dependencies

Add below given dependencies to pom.xml.

<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>

Run below command to download required jars in your local system and update project runtime dependencies also.

mvn eclipse:eclipse

4) Test the application with BasicConfigurator

package com.howtodoinjava;

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

public class Log4jHelloWorld {

	static final Logger logger = Logger.getLogger(Log4jHelloWorld.class);

	public static void main(String[] args)
	{
		//Configure logger
		BasicConfigurator.configure();
		logger.debug("Hello World!");
	}
}

Output:

0 [main] DEBUG com.howtodoinjava.Log4jHelloWorld  - Hello World!

If you saw above message in your console then congrats, we have successfully configured log4j. If you have any problem the repeat all above steps again or drop me a comment.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Tom Moyer

    December 6, 2013

    Please fix the problem first pointed out by Steve Nov 8’th 2012. Your step 3, lines 2 and 3 both need to have two instance of the currently shown as lower case i (the I in Id in groupId and in artifactId) as upper case. The lower case i’s you have cause 4 errors
    1) The groupId can not be empty
    2)’dependencies.dependency.groupId’ for null:log4j:jar is missing
    and a similar pair for the artifactId

    • Lokesh Gupta

      December 7, 2013

      Sorry for the delay. Updated the post.

  2. shmehta21@gmail.com

    November 14, 2013

    Hi Lokesh,
    I did the setup for Maven and after running Maven command as said in step 1.), i got the below error:
    [ERROR] No plugin found for prefix ‘archetype’ in the current project and in the
    plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the
    repositories [local (C:UsersSagarMe.m2repository), central
    n.apache.org/maven2)] -> [Help 1]
    [ERROR] [Help 1] https://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundF
    orPrefixException

    However, I could see some plugings getting downloaded in my .m2 folder

    • Lokesh Gupta

      November 14, 2013

      Paste your pom.xml file here in code tags.

  3. Steve

    November 8, 2013

    good post – couple of typos’…..see below

    1.
    2.log4j (should be groupId)
    3.log4j (should be artifactId)
    4.1.2.17
    5.

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces