I’m trying to get started Log4j in Spring MVC application, but I’m unable to get information, what’s wrong. Each blog post is same: It’s really easy. Just put log4j.properties into /WEB-INF/classes directory. But for me it does not work. The problem is, that there is no place to look for error message. The only I know is, that expected log file was not created. Is there some possibility to debug it? Really to put log4j.properties file in /WEB-INF/classes is enought?
The above mentioned log4j.properties file follows:
#Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\workspace-trainee-actual\\0pokusy\\Sprung\\logik.txt
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=trace, file
Controller using Log4j:
@Controller
public class HelloWorldController {
private Logger log = Logger.getRootLogger();
@RequestMapping("/")
public ModelAndView base() {
log.debug("base URI");
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
}
The only sure fact is, that it work’s, so log is not null and the Log4j library is available.
Workaround
The system works properly allways. The Log4j system is configured for our instance of the Java Virtual Machine. Geronimo has already done it. We can not reconfigure the root logger, but we can use it. The default threshold is INFO and application uses root logger for a debug message. Thus we cannot see it anywhere.
If threshold has decreased to DEBUG, the message appears in Geronimo log. I have changed in the file
$GERONIMO_HOME/var/log/server-log4j.propertiesa line at the beginning:And in
$GERONIMO_HOME/var/log/geronimo.logI can then read:From some unknown reason is neither under Linux nor under Windows created separated output file. We can the message find just in server log, but it does not matter, we can overcome it. Let rename the logger in Log4j configuration:
And in the code accordingly:
private Logger log = Logger.getLogger("springTestLogger");We create the separete log file under Linux easily: