简体   繁体   中英

log4j with tomcat does not log what is logged in mvn test

I have a web application. When I run 'mvn test', it logs the debug messages to console, as I have configured it. But when I deploy it to tomcat, I don't see the the logs of the application. I am absolutely sure that I got the log4j.properties file on the right place in the war, as when I change values in the deployed /var/lib/tomcat7/webapps/worldmodel/WEB-INF/classes/log4j.properties for root logger or for hibernate and touch web.xml, I see/chease to see debug logs for hibernate. But I cannot get my application's debug messages to be logged with any configuration I've tried. Here is how I do the logging:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

logger = Logger.getLogger(BaseObject.class);
log(Level.DEBUG,"message");

Here is log4j.properties for testing:

log4j.rootLogger=INFO, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost = 127.0.0.1
log4j.appender.SYSLOG.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG.Facility = LOCAL0

log4j.logger.org.rulez.magwas.worldmodel=DEBUG
#log4j.logger.org.hibernate.SQL=debug 
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type=trace 

here is the log4j.properties which gets deployed:

log4j.rootLogger=INFO, SYSLOG

log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost = 127.0.0.1
log4j.appender.SYSLOG.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG.Facility = LOCAL0

log4j.logger.org.rulez.magwas.worldmodel=DEBUG
#log4j.logger.org.hibernate.SQL=debug 
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type=trace 

I get such lines in mvn test:

2013-05-21 07:12:17,751 [main] DEBUG org.rulez.magwas.worldmodel.BaseObject- setValue0e9é 072r 074t 0e9é 06bk

The whole project: https://github.com/magwas/worldmodel/commit/c6b08da0a733d9b61257c669e0cc4af9e59444be

edit:

ok, forget it, the code (getter and setter methods in a bean) seems not being been called, perhaps hibernate sets/gets the values directly?

Since, you're configuring your ROOT logger with level INFO it won't display any DEBUG level messages.

log4j.rootLogger=INFO, SYSLOG

The above rootLogger should be configured as DEBUG to see debug and other higher levels (Fatal, Error, Warn and Info) in your logs.

log4j.rootLogger=DEBUG, SYSLOG

I'm not sure what your Maven test case configuration is but perhaps the log4j.properties you've shared isn't having any effect there.

EDIT : It seems from your edit that your logging in production is misconfigured. If your logging is set to INFO it should not log DEBUG messages. I mean, that simply defeats the purpose of having log levels, clearly shows Log4j has not been configured properly and drains Prod resources both space and time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM