Blog Image

Log4j Tool  

Log4j is a logging framework which enables us to perform logging in our project. By using logging, we can track control flow in our project. The log4j API strives to be simple to understand and to use.

Why logging?

There are several uses of log4j. One is to get the flow of execution of control within our application. Also, we can track the exceptions and errors to know what went wrong by following the logs. We can also track the control flow by using other process (i.e. by adding ?System.Out.Println? inside the classes present in application or by putting debug points). But this processes are costliest operation as it uses Input and Output streams and its shows the result in console. For different environments like STAGE and PRODUCTION we cannot use debugger and the result generated in console cannot stay longer time span and cannot be keep for future reference purpose. As human interruption is there so there might some chance that something goes wrong and which will kill the performance of our application. So, to avoid this kind of interventions log4j introduced.

Architecture of log4j:

Log4j Levels and Visibility:

ALL: The ALL has the lowest possible rank and is intended to turn on all logging.

DEBUG: The DEBUG Level logs informational events that are most useful to debug an application.

ERROR: The ERROR level logs error events that might still allow the application to continue running.

FATAL: The FATAL level logs very severe error events that will presumably lead the application to abort.

INFO: The INFO level logs informational messages that highlight the progress of the application at coarse-grained level.

OFF: The OFF has the highest possible rank and is intended to turn off logging.

TRACE: The TRACE Level logs informational events than the DEBUG

WARN: The WARN level logs potentially harmful situations.

Note: - The vertical line in the below chart is display the levels and horizontal line is displaying the visibility of the level as per the logger configuration.

Steps for creating log4j program using maven:

Step:1: Generate a project using maven command.


mvn archetype:generate -DgroupId=org.stackspace -DartifactId=log4jTool -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart 

Note: - After generating the project we need to add ?resource? directory manually in the ?main and test? under src directory.

Step-2: Import this generated maven project to the work-space.

File -> Import -> choose existing maven project from the select window -> choose your recent created project directory by clicking on browse -> click on next

After import the project the directory structure will be looks as below

Step-3: Add the log4j jar dependency in pom.xml

Log4j dependency: -


While writing this, I used log4j-1.2.16.jar.

Note: - After adding the dependency in pom.xml update the project by ?right click on project -> choose MAVEN from the list -> update project? (Shortcut key for this whole process is ?ALT+F5?).

Step-4: Add the log4j configuration file (i.e. ? in the resource directory.

Ex: -

Below is a sample of log4j configuration file with different appenders(i.e. console and file appenders).

File name:


#Consoleappender logger.
log4j.appender.stdout.layout.ConversionPattern=[%d] [%t] [%M] [%m] [%20c] %n

#Fileappender logger for all level.
log4j.appender.file.layout.ConversionPattern=[%d] [%t] [%M] [%m] [%20c] %n

#Fileappender logger for error level
log4j.appender.error_file.layout.ConversionPattern=[%d] [%t] [%M] [%m] [%20c] %n

Write any class and check the code

Below is a small snippet of code for reference:

File name:

 public class FYDTest {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(FYDTest.class);"Logger created successfully.");
        logger.error("Logging error message.");

tags: log4j

About author

User Image

I am a software developer who love programming and passionate about new technologies. I like computer games. There is a say from my side i.e. "code and play". When ever I get time I like to code or to play computer games.



Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.