Spring MVC and Log4j

log4jmodel-view-controllerspring

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.

Best Answer

Try adding

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

to your web.xml file

Related Topic