Java 回写:消除isn';t在后台工作

Java 回写:消除isn';t在后台工作,java,slf4j,logback,Java,Slf4j,Logback,我正在做一个项目,我使用slf4j和logback进行日志记录。问题是日志的级别是动态的,由用户选择。因此,用户也可以停止日志记录,这样他就可以获得空间(我们记录了很多东西,如果流量很大,很快就会非常昂贵) 为此,我将一些变量保存在属性文件中,每次更改时都重新加载它。但我并没有像上面所说的那个样,成功地使它和leveloff(用于禁用日志)一起工作 这是我的logback.xml: <?xml version="1.0" encoding="UTF-8"?> <configu

我正在做一个项目,我使用slf4j和logback进行日志记录。问题是日志的级别是动态的,由用户选择。因此,用户也可以停止日志记录,这样他就可以获得空间(我们记录了很多东西,如果流量很大,很快就会非常昂贵)

为此,我将一些变量保存在属性文件中,每次更改时都重新加载它。但我并没有像上面所说的那个样,成功地使它和leveloff(用于禁用日志)一起工作

这是我的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>

根据您提供的logback状态消息,根记录器将按预期设置为水平。然而,名为“http”的记录器被设置为DEBUG,我怀疑这是问题的根源


在任何情况下,从不同的记录器引用相同的appender都是一个坏主意。此外,配置文件会执行两次,附加程序名为“generalFile”和“errorFile”同样,从不同的记录器引用同一个appender是一个坏主意,不受支持。

状态消息会说什么?请看详细情况。在我的问题上能得到你的帮助是我的荣幸。我已经在我的问题中添加了日志。谢谢,它正在工作。现在我遇到了一个问题,因为当我从记录器http中删除appender
generalFIle
console
时,我在“generalFIle”和“console”中丢失了
http
日志。在名为http的记录器中,将additivity设置为true,或者只是删除additivity属性,它将默认为true。
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