Spring-Hibernate logging - separate log files

こ雲淡風輕ζ 提交于 2021-01-28 10:33:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!