Friday, June 12, 2015

Log4J2 logging on JSP (In tomcat 7)

In this post I will explain steps to enable log4J2 logging on JSP files in web project. From the same way you can add this logs in to Java classes as well. It's highly recommend not to use logs on jsp unnecessary because it may lead to performance issue on your web application.

Prerequisites 
  1. You should have install java in your PC and set path variable correctly (JAVA_HOME, JRE_HOME)
  2. You should have install Eclips Java EE IDE in your PC
  3. You should have Apache Tomcat in your machine and it should be added to Run Time Environments in Eclips

Step 1 Create Dynamic web project
To Create dynamic web project go to File->New->Dynamic Web Project and give preferred name for the project and click finish. Then add a index.jsp file in to web content folder and add following content in to that file.

Content to index.jsp file

<html>
    <head>
        <title>Demonstration log4j usage in jsp</title>
    </head>
    <body>
        <b>The log messages are shown save in the ${catalina.home}/logs/app.log file.</b>
    </body>
</html>

Then right click on project and give run on server then you can see similar screen to Figure 1.

Figure 1 : When projects run on server content in index.jsp will be shown similar to this screenshot 

 Step 2 Add log4J2 libraries and configurations 
Then go to the following page https://logging.apache.org/log4j/2.0/download.html and download apache-log4j-2.3-bin.zip. Then extract the content in to folder.

Then right click on WEB-INF folder inside the WebContent folder add log4J2.xml with following configurations.

log4J2.xml file content

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
  <appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>

    <File name="MyFile" fileName="${sys:catalina.home}/logs/app.log">
        <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>             
  </appenders>

  <loggers>     
<root level="debug">
       <appender-ref ref="MyFile" level="error"/>
       <appender-ref ref="MyFile" level="fatal"/>
       <appender-ref ref="MyFile" level="warn"/>            
    </root>    
  </loggers>
</configuration>

Then we have to add "log4j-api-2.3.jar" , "log4j-core-2.3.jar" files from downloaded jar files in to lib folder as show in Figure 2.Then right click on the project and Build Path -> Configure Build Path and add two Jar files in to libraries. (Click on add jar and select the two jars in lib folder)

Figure 2 : Two jar files in lib folder inside WEB-INF

Figure 3: Add two jar files in to libraries

Then click on Source tab in same interface and add WEB-INF folder in to build path. You can add folder by click on Add folder and select the WEB-INF folder.

Figure 4 : Add WEB-INF in to build path
Now lets add some logs in to index.jsp page in Web content folder. Replace index.jsp file with following content.

<%@ page import="org.apache.logging.log4j.Logger" %>
<%@ page import="org.apache.logging.log4j.LogManager" %>
<html>
    <head>
        <title>Demonstration log4j usage in jsp</title>
    </head>
    <body>
        <% Logger log = LogManager.getLogger(this.getClass());
           log.info("Show INFO message");
           log.warn("Show WARN message");
           log.error("Show ERROR message");
           log.fatal("Show FATAL message"); %>
        <b>The log messages are shown save in the ${catalina.home}/logs/app.log file.</b>
    </body>
</html>

Step 3 Do necessary changers on Tomcat 7

Here we just need to add "log4j-api-2.3.jar" , "log4j-core-2.3.jar" files in to tomcat lib folder.(As show in Figure 5)

Figure 5: Add jars in to Tomcat lib folder

Now we have completed all the configurations so now lets run the project by right click on project and go to Run As->Run On Server. You will see screen similar to Figure 6 and if you go to Tomcat installation directory -> logs folder then you can see app.log file with similar content to Figure 7

Figure 6: Interface when run the project on Eclips
Figure 7: Logs are written in to app.log file

These are the basic steps to add log4j2 in to web application and put logs in JSP but it's highly recommend to not to add logs in to JSP files since those are loaded in to client side if there are lots of logs in jsp's then there might be performance issue arises. It's better to use logs only if necessary to use in jsps.

No comments:

Post a Comment