问题
I am using Spring & Hibernate in my application and I want to set up two different log files - one main log file all the non-sql logging and another file for writing sql queries. This is my log4j.properties file.. But while the sql log file is working as expected..the issue is that the main log file also contains sql queries.. What changes should I make here to to stop writing sql queries to main log file..
#application log properties
log4j.appender.rollingFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.rollingFile.File = C:\\myapp\\logs\\app.log
log4j.appender.rollingFile.Threshold = TRACE
log4j.appender.rollingFile.DatePattern = '.'yyyy-MM-dd
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d %5p [%C:%M:%L] - %m%n
log4j.rootLogger = ERROR, rollingFile
#hibernate properties
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File = C:\\myapp\\logs\\sql.log
log4j.appender.file.Threshold = TRACE
log4j.appender.file.DatePattern = '.'yyyy-MM-dd
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%C:%M:%L] - %m%n
log4j.logger.jdbc.sqltiming = DEBUG, file
回答1:
This is the additivity behaviour of the appenders in log4j .
Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.
Just set the additivity for the logger jdbc.sqltiming
to false and its log messages will not be propagated to its parent loggers' appenders (i.e. app.log)
log4j.additivity.jdbc.sqltiming = false
References:
See the section Appenders and Layouts for more information about Appender Additivity
来源:https://stackoverflow.com/questions/7044892/spring-hibernate-logging-separate-log-files