Web Application and Log4J

This may be useful for programmers of Java web application and log4j. Mostly, we get stuck up with some issue, and don’t come to know what is actually happening inside, and end up getting some error (may be 500 internal server error) on screen, or sometimes a blank screen, even one line of exception. Then, the struggle starts to find out what is actually happening. System.out.println (SOPs) clog the console. When it comes to deploying on a web server, where you do not have access to the console, the real trouble starts. Here debugging becomes nightmare. Newbies end up spending hours, and waiting for some expert to come to their desk to get a simple suggestion – enable logging using Log4J. After adding logs the problem is cleared in minutes.

In short, it is better to have good logging enabled in application, so that the developer gets enough information to debug a problem. In this article, I am assuming that the application is already running without usage of Log4J. Now you want to add logging to it. What all you need to do. It is very simple. Below are the simple steps.

Web Application and Log4j Integration:

1. Download L0g4J latest version from Log4J site. This is going to be a jar file. Add it to the application library. Mostly it is ../WEB-INF/lib directory. If you are using Eclipse IDE, add this jar to the build path libraries.

Web Application and Log4J

2. Add log4j.properties file to such a location, that after build it gets copied to ../WEB-INF/classes directory in web archive (.war file). Suppose under source (or src), you have resources folder, you can add this file to it, add the resources folder to the Source folder in Eclipse build path. This will ensure copying of this file to the desired location. Also, in the sample log4j.properties below, change the file path to a location where you (actually the web server on which application is getting deployed) has access.

log4j.rootLogger=DEBUG, stdout, file
log4j.logger.org.apache.struts=TRACE, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{dd MM yyyy HH:mm:ss,SSS} %5p [%t] (%-50.50C:%M:%L) - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss.SSS} %r %5p [%t] %x (%C:%M:%L) - %-50m%n

log4j.appender.file.File=D:\app.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=1

3. Deploy application on web server and start it. It should generate the log file at desired location.

4. You can play around with the details of log statement by changing configurations. Specific logging can also be enabled for certain technologies e.g. Struts, Spring, Hibernate, Apache Commons etc. Below are few samples. (Note, Spring also allows enabling logs using context listener configuration in web.xml, we leave it for those who are interested in it to learn from Spring’s documentation.)

log4j.logger.org.apache.struts=TRACE, stdout, file

log4j.logger.org.apache.commons=INFO, stdout, file
log4j.additivity.org.apache.commons=false

log4j.logger.org.springframework=DEBUG, stdout, file
log4j.additivity.org.springframework=false

log4j.logger.org.hibernate.type=DEBUG, stdout, file
log4j.additivity.org.hibernate.type=false
log4j.logger.org.hibernate.transaction=DEBUG, stdout, file
log4j.additivity.org.hibernate.transaction=false
log4j.logger.org.hibernate.jdbc=DEBUG, stdout, file
log4j.additivity.org.hibernate.jdbc=false
log4j.logger.org.hibernate.cache=DEBUG, stdout, file
log4j.additivity.org.hibernate.cache=false
log4j.logger.org.hibernate.pretty=DEBUG, stdout, file
log4j.additivity.org.hibernate.pretty=false
log4j.logger.org.hibernate.persister.entity=DEBUG, stdout, file
log4j.additivity.org.hibernate.persister.entity=false
log4j.logger.org.hibernate.SQL=DEBUG, stdout, file
log4j.additivity.org.hibernate.SQL=false

If you encounter following statement on your web server console or inside stdout log, then it is very much possible that the log4j.properties is not properly initialized, and log4j is using the default file from log4j.jar.

log4j:WARN Please initialize the log4j system properly.

Be the first to comment

Leave a Reply

Your email address will not be published.


*