我正在开发一个项目,我使用slf4j和logback进行日志记录。事情是日志级别是动态的并且由用户选择。因此,用户也可以停止记录,这样他就可以获得太空(我们记录了许多东西,如果流量很大,它可能会很快成本很高)。
为此,我将一些变量保存在属性文件中,并在每次更改时重新加载。但是我没有成功地将它与关闭级别(用于禁用日志)一起工作,如the doc所述。
这是我的logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false">
<!-- We don't need the logback.xml to be checked every time, we will trigger in java
the reload if we need it -->
<property file="/opt/program/log.properties"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${PATTERN_LOGS}</pattern>
</encoder>
</appender>
<appender name="generalFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_GENERAL_PATH}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_GENERAL_FILENAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>${PATTERN_LOGS}</pattern>
</encoder>
</appender>
<appender name="httpFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HTTP_PATH}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HTTP_FILENAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>${PATTERN_LOGS}</pattern>
</encoder>
</appender>
<appender name="errorFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_ERROR_PATH}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ERROR_FILENAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>${PATTERN_LOGS}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<logger name="http" level="DEBUG" additivity="false">
<appender-ref ref="httpFile"/>
<appender-ref ref="generalFile"/>
<appender-ref ref="console"/>
</logger>
<root level="${LEVEL_ROOT}">
<appender-ref ref="console"/>
<appender-ref ref="generalFile"/>
<appender-ref ref="errorFile"/>
</root>
</configuration>
这是我在激活状态消息时从控制台获得的内容:
00:06:45,861 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
00:06:45,861 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
00:06:45,862 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/root/Documents/project/projet%20OSM/Tomcat%20Installation/wtpwebapps/SekaiServer/WEB-INF/classes/logback.xml]
00:06:45,944 |-INFO in ch.qos.logback.classic.joran.action.LoggerContextListenerAction - Adding LoggerContextListener of type [ch.qos.logback.classic.jul.LevelChangePropagator] to the object stack
00:06:45,961 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@3b0933e7 - Propagating DEBUG level on Logger[ROOT] onto the JUL framework
00:06:45,962 |-INFO in ch.qos.logback.classic.joran.action.LoggerContextListenerAction - Starting LoggerContextListener
00:06:45,962 |-INFO in ch.qos.logback.classic.joran.action.JMXConfiguratorAction - begin
00:06:45,968 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
00:06:45,975 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
00:06:45,993 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
00:06:46,091 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
00:06:46,100 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [generalFile]
00:06:46,143 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
00:06:46,148 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern /opt/program/log/general.%d{yyyy-MM-dd}.log for the active file
00:06:46,155 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/opt/program/log/general.%d{yyyy-MM-dd}.log'.
00:06:46,155 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
00:06:46,156 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sat Mar 26 00:03:15 CET 2016
00:06:46,159 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
00:06:46,165 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[generalFile] - Active log file name: /opt/program//log/general.log
00:06:46,165 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[generalFile] - File property is set to [/opt/program//log/general.log]
00:06:46,167 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
00:06:46,168 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [httpFile]
00:06:46,170 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
00:06:46,170 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern /opt/program/log/http.%d{yyyy-MM-dd}.log for the active file
00:06:46,171 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/opt/program/log/http.%d{yyyy-MM-dd}.log'.
00:06:46,171 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
00:06:46,172 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sat Mar 26 00:03:15 CET 2016
00:06:46,172 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
00:06:46,174 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[httpFile] - Active log file name: /opt/program//log/http.log
00:06:46,174 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[httpFile] - File property is set to [/opt/program//log/http.log]
00:06:46,174 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
00:06:46,175 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [errorFile]
00:06:46,177 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
00:06:46,177 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern /opt/program/log/error.%d{yyyy-MM-dd}.log for the active file
00:06:46,178 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/opt/program/log/error.%d{yyyy-MM-dd}.log'.
00:06:46,178 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
00:06:46,178 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Thu Mar 24 19:50:38 CET 2016
00:06:46,179 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
00:06:46,190 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[errorFile] - Active log file name: /opt/program//log/error.log
00:06:46,190 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[errorFile] - File property is set to [/opt/program//log/error.log]
00:06:46,192 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [http] to DEBUG
00:06:46,192 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@3b0933e7 - Propagating DEBUG level on Logger[http] onto the JUL framework
00:06:46,192 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [http] to false
00:06:46,192 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [httpFile] to Logger[http]
00:06:46,194 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [generalFile] to Logger[http]
00:06:46,194 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[http]
00:06:46,194 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to OFF
00:06:46,194 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@3b0933e7 - Propagating OFF level on Logger[ROOT] onto the JUL framework
00:06:46,194 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
00:06:46,194 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [generalFile] to Logger[ROOT]
00:06:46,194 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [errorFile] to Logger[ROOT]
00:06:46,195 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
00:06:46,195 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@78865c1d - Registering current configuration as safe fallback point
答案 0 :(得分:1)
根据您提供的回溯状态消息,根记录器按预期设置为关闭级别。但是,名为“http”的记录器设置为DEBUG,我怀疑这是问题的根源。
无论如何,从不同的记录器引用相同的appender是个坏主意。此外,配置文件执行两次,其中appender名为'generalFile'和'errorFile'。 同样,从不同的记录器引用相同的appender是一个坏主意,并且不受支持。