Wednesday 26 June 2013

Log4j Configuration

Steps to configure log4j in maven web application


Step: 1


   Add the required dependencies in pom.xml
                 

Step: 2

   Add log4j.xml in your application root folder.
   If you are using any IDE put log4j.xml in "src/main/resources". After compilation, it will be copied to "WEB-INF/classes" folder. Copy and paste the following contents in log4j.xml


I am using glassfish as the underlying server that's why the log folder relative path is "com.sun.aas.instanceRoot". For other servers, get the log folder path and replace that line. We are using RollingFileAppender, that enables us to create log files based on rolling policy. In other words, based on the date stamp it rolls at that time, 
            default is yyyy-MM-dd, (rolls at midnight)
See:http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html
So, instead of single file, It generates log files on daily basis.


Step: 3


    Add Log4jConfigListener in web.xml
    

To debug your java files
   
That's it! Now build and run the project.

No comments:

Post a Comment