10/12/15

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

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 -->
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>${org.slf4j-version}</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>${org.slf4j-version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>${org.slf4j-version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.15</version>
   <exclusions>
    <exclusion>
     <groupId>javax.mail</groupId>
     <artifactId>mail</artifactId>
    </exclusion>
    <exclusion>
     <groupId>javax.jms</groupId>
     <artifactId>jms</artifactId>
    </exclusion>
    <exclusion>
     <groupId>com.sun.jdmk</groupId>
     <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
     <groupId>com.sun.jmx</groupId>
     <artifactId>jmxri</artifactId>
    </exclusion>
   </exclusions>
   <scope>runtime</scope>
  </dependency>

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" />
</layout>
</appender>

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

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

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

</log4j:configuration>

Explanation

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