Adding log4j logger to Core Java application, Web Project or Spring project

Hi Readers,

In this tutorial i am describing how ti integrate logger into your web application, spring project or java application.


log4j is Apache Software Foundation product. It allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files. Best of all, log4j has a gentle learning curve.
Almost every application requires logging of info, debug and error messages for later use. log4j API provides very fine grain control while integrating it in your application and various features. Your java programming is almost impossible without logging.

Log4j Logging levels

ALL           The ALL has the lowest possible rank and is intended to turn on all logging.
DEBUG           The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
ERROR          The ERROR level designates error events that might still allow the application to continue running.
FATAL          The FATAL level designates very severe error events that will presumably lead the application to abort.
INFO           The INFO level designates 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 designates finer-grained informational events than the DEBUG
TRACE_INT           TRACE level integer value.
WARN          The WARN level designates potentially harmful situations.

I will recommend to set your logger at Debug level. It is up to you how in depth messages you want to see.

log4j Implementation

Here i am assuming that you are using Maven build tool. So if you are using some other build tool then you can add dependancies accordingly.

So here are the steps :-

Step 1:- You need to add given dependancies

<!-- Logging -->

Step 2:- Add log4j.xml file to your project classpath

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />

<!-- Application Loggers -->
<logger name="com.projectname.my.packagename">
<level value="debug" />

<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="debug" />

<!-- Root Logger -->
<priority value="debug" />
<appender-ref ref="console" />



  • Under appender comment you have code that will put logs generated by script to console output. With hep of this code you can see logs on console screen.
  • Application loggers will log what you want to log inside your application's source code. You need to provide package name under which your source code is present.
  • 3rd party loggers can log the dependancies that you have added in your project. In this i am logging spring.
  • At last root logger will log your root level actions 

Step 3:- Implement logging in your source code

In your classes you add given line on top

private final static Logger LOGGER = LoggerFactory.getLogger(YourClass.class);

And inside your method you need to log like

logger.error("error", e);

Here you can log your error object. You can use logging levels to log various types of messages, info, debugging and errors.

Like, comment and also put your queries.