Set “log4j” Apache Tomcat

Pubblicato da lentux-informatica il

  •  
  •  
  •  
  •  
  •  

After battling with several different configurations when it comes to logging and Tomcat 5.5.x, I decided to put together this guide for a simplistic approach.

This assumes that you would like to use Log4J for both Tomcat’s internal logging as well as your deployed web application’s logging needs.




If you use Maven, follow these steps:

  1. Create a file log4j.properties inside src/main/resources.
  2. Add dependency Maven for log4j. I used version 1.2.14.
  3. Copy this inside you log4j.properties file:

log4j.rootLogger=DEBUG, R, C

#RollingFileAppender – scrive sul file tomcat.log come indicato sotto
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/tomcat.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{MM/dd HH:mm:ss} %-5p %30.30c %x – %mn
#Console Appender – scrive sulla console di eclipse
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n

 

Where appender.R write your logs inside de file "tomcat.log" 
and appender.C write indide Eclipse console.

The pattern itself is defined as follows:

  1. %5p refers to the type of log entry. This would appear in the  log file as INFO, DEBUG, ERROR, etc.    Technically, %p would be  enough to include this description; the 5 is there to include  the word in a  5-character width column.
  2. %d refers to the date.
  3. %t does not refer to the time but to the name of the thread that raised this log entry. In the example above, this  code is enclosed in square brackets which are used in the log  entry.
  4. %c lists the category that generated this log which usually  is the class name.
  5. %m displays the message
  6. %n adds a carriage return.

Redeploy your project and it work…


  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •