tomcat logging using log4j

In normal install tomcat use JULI for logging and this logging framework has issues on some advance rotation log file  configuration. I got issue with my hard disk always full after tomcat run for a couple of week cause log files. So i wonder if i can limit number of log files generate and tomcat reuse the old one when limit is reach.
After some research i find a solution. Tomcat allow us integration with log4j logging framework so we can handle above issue so easy.
If you have same problem i can guide you through.
Steps to make Tomcat work with log4j

We download two files from extras on tomcat download section

  • tomcat-juli.jar
  • tomcat-juli-adapters.jar

Copy tomcat-juli.jar to $CATALINA_HOME\bin to replace old one

Copy tomcat-juli-adapters.jar to $CATALINA_HOME\lib

Delete logging.properties in $CATALINA_HOME\conf

Create log4j.properties in $CATALINA_HOME\libs. This is sample of mine:

log4j.rootLogger=INFO, CATALINA

log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
#log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.log
log4j.appender.CATALINA.File=D:/catalina.log
log4j.appender.CATALINA.Encoding=UTF-8
log4j.appender.CATALINA.MaxBackupIndex=15
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern=%d [%t] %-5p %c- %m%n
log4j.appender.CATALINA.MaxFileSize=10MB

Download log4j version greater than 1.2 and copy to $CATALINA_HOME\lib

After that restart tomcat.

 

Leave a Reply