开发者

Spring MVC and Log4j

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.


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


I have changed logging threshold of Geronimo to debug and I found '2011-06-24 07:33:18,353 DEBUG [root] base URI'. I thing there is some conflict. The application is one process, one JVM instance with thousands of classes, but with only one root logger for all of them.

Seams that you are right:

From the Geronimo Documentation:

Note that in any case, unless you use hidden-classes or inverse-classloading to load your own copy of log4j separate from the geronimo copy, log4j will not automatically read any log4j.properties files you may have included in your classpath.

I found the last "solution" from the documentation very interesting:

Copy the log4j.properties file by hand to the appropriate location such as var/my-app/log4j.properties. There is no need to include this file in your app.

Because that allows you to externalize the log4j configuration. So the Operations-Guy can manage and change the log4j configuration. And you do not need to build/and deploy a new version if for example the directory where the files are stored is changed.


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.properties a line at the beginning:

log4j.rootLogger=DEBUG, CONSOLE, FILE
And in $GERONIMO_HOME/var/log/geronimo.log I can then read:
2011-06-24 20:02:21,375 DEBUG [root] base URI
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:
#Root logger for application
log4j.logger.springTestLogger=TRACE, APLOK
And in the code accordingly:
private Logger log = Logger.getLogger("springTestLogger");
We create the separete log file under Linux easily:
cat $GERONIMO_HOME/var/log/geronimo.log|grep springTestLogger > separe.log


Try with

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

 <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜