Set “log4j” Apache Tomcat
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:
- Create a file log4j.properties inside src/main/resources.
- Add dependency Maven for log4j. I used version 1.2.14.
- Copy this inside you log4j.properties file:
log4j.rootLogger=DEBUG, R, C
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:
- %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.
- %d refers to the date.
- %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.
- %c lists the category that generated this log which usually is the class name.
- %m displays the message
- %n adds a carriage return.
Redeploy your project and it work…