Implement log4j using Maven and JUnit

Logging is very important for any project. Let’s see the steps to implement in your maven project.

Step 1: Add dependency in pom.xml file

Step 2: Create file. Generally it comes under src/test/resources

Step 3: Implement it in Java class

Step 4: Call you Runner Junit class

Now, run your junit class and see the log in both console and log file.

More on Logging
1. How to implement logging(log) in Java
2. Examples

Please share your feedback. Also, contact at for more details. Examples

I don’t find any good example of so adding some log4j properties files.

For tests, the directory src/test/resources is the appropriate location for the log4j.propertiesfile. When Maven starts the tests, this directory will be part of the classpath.

The file needs to go into src/main/resources/ or src/test/resources/ (if you need it only for unit tests).
Longer explanation: Maven splits the Java classpath into source code and resources (non-Java things like property files, images, etc). The main reason for this is that Maven can do filtering of resources for you and to make this extra safe, they put the resources into a different folder than the sources.

1. Output to Console

All logging will be redirected to your console.

2. Output to File

All logging will be redirected to your specified log file.

3. Output to Console and File

All logging will be redirected to both log file and console.

How to implement logging(log) in Java

Logging is very important when publishing any project in the real world. It is used to log the important messages or error in the log file.

A Logger object is used to log messages to a specific system or application component.

  1. Create a Log class where we are creating log.txt file and assigning appenders.


2. class is used to see how messages and errors are caught.

This is the simple way to write the logging using java.util.logging package.