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?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “Log4j Maven Configuration Example”

  1. 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

    Reply
  2. 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

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.