简体   繁体   中英

Get spring messages from sysout to file using log4j

I'm trying to get Spring messages redirected to a log file.

I am able to get only some of the messages but not all of them.

This is my log4j file

log4j.rootLogger=DEBUG, stdout, file, fileDebug
log4j.logger = ERROR
log4j.category.org.springframework = DEBUG
log4j.category.org.springframework.jdbc.core = DEBUG
org.springframework.jdbc.core.JdbcTemplate = DEBUG
org.springframework.jdbc.core.StatementCreatorUtils = DEBUG

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file INFO level
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.Threshold=INFO
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file DEBUG level
log4j.appender.fileDebug=org.apache.log4j.RollingFileAppender
log4j.appender.fileDebug.Threshold=DEBUG
log4j.appender.fileDebug.MaxFileSize=5MB
log4j.appender.fileDebug.MaxBackupIndex=10
log4j.appender.fileDebug.layout=org.apache.log4j.PatternLayout
log4j.appender.fileDebug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# log locations
log4j.appender.file.File=logInfo.log
log4j.appender.fileDebug.File=debug.log

What I get in my debug.log is for example

2015-04-14 11:18:16 DEBUG ConfigurationImpl:120 - Setting custom MessageInterpolator of type org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator 2015-04-14 11:18:16 DEBUG ConfigurationImpl:140 - Setting custom ConstraintValidatorFactory of type org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory 2015-04-14 11:18:16 DEBUG ConfigurationImpl:154 - Setting custom ParameterNameProvider of type com.sun.proxy.$Proxy15 2015-04-14 11:18:16 DEBUG ValidationXmlParser:88 - Trying to load META-INF/validation.xml for XML based Validator configuration. 2015-04-14 11:18:16 DEBUG ValidationXmlParser:95 - No META-INF/validation.xml found. Using annotation based configuration only.

But I don't see there anything from jdbcTemplate but it is in my console.

11:19:19.673 [http-bio-8080-exec-6] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query

So this should be debug level for org.springframework.jdbc.core.JdbcTemplate, but that doesn't seem to do anything.

I am probably missing something obvious but I just can't see it... Thanks for advice.

You need to Change following lines:

From

org.springframework.jdbc.core.JdbcTemplate = DEBUG
org.springframework.jdbc.core.StatementCreatorUtils = DEBUG

To

log4j.logger.org.springframework.jdbc.core.JdbcTemplate = DEBUG, fileDebug
log4j.additivity.org.springframework.jdbc.core.JdbcTemplate=false

log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils = DEBUG, fileDebug
log4j.additivity.org.springframework.jdbc.core.StatementCreatorUtils=false

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