Reputation: 3166
I have a web application running in a vanilla install of Tomcat 7 on Windows with the internal Tomcat logging left unchanged (default). In my application I use log4j which has a RollingFileAppender writing to my own application log (app.log) in the TOMCAT/logs directory (not the localhost.yyyy-mm-dd.log). Logging seems to work fine for my application except that some stack traces do not show up in app.log and instead show up in the localhost.yyyy-mm-dd.log. For example, my application was missing commons-validators classes and this stack trace did not show in app.log.
I'm not sure how to get these stack traces to show up in my app.log? My log4j.properties file is here:
### Root Level ###
log4j.rootLogger=WARN, LOGFILE
### Application Level ###
log4j.logger.com.ccn=TRACE
### Spring ###
log4j.logger.org.springframework.core=INFO
log4j.logger.org.springframework.beans=INFO
log4j.logger.org.springframework.context=INFO
log4j.logger.org.springframework.web=INFO
### Configuration for the LOGFILE appender ###
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.MaxFileSize=5MB
log4j.appender.LOGFILE.MaxBackupIndex=10
log4j.appender.LOGFILE.File=$\{catalina.home\}/logs/app.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=[%p %d %t] %c [%C{1}.%M(): "%m"]%n
Upvotes: 0
Views: 7421
Reputation: 3166
I found the problem. I'm using a org.springframework.stereotype.Controller annotation to mark my controller class. The specific exceptions that I was losing were being generated in this class. The solution was to add a @ExceptionHandler
annotation to a method in the class in which I could redirect the exception to Log4j similar to this:
@ExceptionHandler
public void handleException(Exception e) {
logger.error("Error: ", e);
}
Without the @ExceptionHandler
decorated method the stack traces were being spit out to stderr which ended up in the localhost.yyyy-mm-dd.log.
Upvotes: 1
Reputation: 532
How do you do stack trace logging?
In my case,
Logger log = Logger.getLogger(HogeServlet.class);
try {
// Something
} catch (HogeException e) {
log.error("Hoge Error !!", e); // Print stack trace
}
Upvotes: 0