Logback扫描无法正常工作

时间:2015-02-14 00:43:39

标签: java debugging logback

我无法使用logback的自动扫描功能工作。它似乎没有发现变化。我已经将section =“true”添加到section并读取它的输出,一切似乎都很好。它清楚地说明了每30秒监视logback.xml文件的变化。我不知道为什么没有采取改变措施。只是寻找一些指导我如何进一步解决这个问题。谢谢。

我应该提一下,logback正好记录到文件,只是没有接收到对logback配置文件的更改。

logback.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds" debug="true">

    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  

    <property name="LOGS_PATH" value="C:\\Users\\****\\Desktop\\css_dev\\q_logs" />

    <appender name="scheduledTasksAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS_PATH}/scheduledTasks.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGS_PATH}/scheduledTasks.%i.log.zip</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>25</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>5MB</maxFileSize>
        </triggeringPolicy>

        <encoder>
            <pattern>%d{MMM/dd/yyyy HH:mm:ss} %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="tasks" level="warn" additivity="false">
        <appender-ref ref="scheduledTasksAppender"/>
    </logger>
</configuration>

Web应用程序启动时输出到Tomcat日志:

|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/*****/Desktop/css_dev/ProjectQ/build/web/WEB-INF/classes/logback.xml]
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds
|-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[C:\Users\*****\Desktop\css_dev\ProjectQ\build\web\WEB-INF\classes\logback.xml]] every 30 seconds. 
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
|-INFO in ch.qos.logback.core.joran.action.StatusListenerAction - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
|-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/*****/Desktop/css_dev/ProjectQ/build/web/WEB-INF/classes/logback.xml]
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds
|-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[C:\Users\*****\Desktop\css_dev\ProjectQ\build\web\WEB-INF\classes\logback.xml]] every 30 seconds. 
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
|-INFO in ch.qos.logback.core.joran.action.StatusListenerAction - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [scheduledTasksAppender]
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [scheduledTasksAppender]
|-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@298f464e - Will use zip compression
|-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@298f464e - Will use zip compression
|-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@298f464e - Large window sizes are not allowed.
|-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@298f464e - Large window sizes are not allowed.
|-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@298f464e - MaxIndex reduced to 21
|-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@298f464e - MaxIndex reduced to 21
|-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
|-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
|-INFO in ch.qos.logback.core.rolling.RollingFileAppender[scheduledTasksAppender] - Active log file name: C:\Users\*****\Desktop\css_dev\q_logs/scheduledTasks.log
|-INFO in ch.qos.logback.core.rolling.RollingFileAppender[scheduledTasksAppender] - Active log file name: C:\Users\*****\Desktop\css_dev\q_logs/scheduledTasks.log
|-INFO in ch.qos.logback.core.rolling.RollingFileAppender[scheduledTasksAppender] - File property is set to [C:\Users\*****\Desktop\css_dev\q_logs/scheduledTasks.log]
|-INFO in ch.qos.logback.core.rolling.RollingFileAppender[scheduledTasksAppender] - File property is set to [C:\Users\*****\Desktop\css_dev\q_logs/scheduledTasks.log]
|-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [tasks] to WARN
|-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [tasks] to WARN
|-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [tasks] to false
|-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [tasks] to false
|-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [scheduledTasksAppender] to Logger[tasks]
|-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [scheduledTasksAppender] to Logger[tasks]
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
|-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5b63e18f - Registering current configuration as safe fallback point
|-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5b63e18f - Registering current configuration as safe fallback point

5 个答案:

答案 0 :(得分:8)

更新(2019年5月):该错误现在似乎已修复。

=============================================== ==============================

使用logback 1.1.7,需要明确提及obj。否则,logback将不会扫描更改。这是由于最近引入的错误(Here是链接)。

答案 1 :(得分:3)

实际上扫描确实有效,但是扫描是在target / build目录中的logback.xml上完成的,这并不是真的有用......

答案 2 :(得分:3)

我遇到了类似的问题,根本原因证明是我初始化logback的方式。

初始配置 - 无效:

下面是我用Joran配置回溯的代码。

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
InputStream is = new FileInputStream(logConfigPath); // 'logConfigPath' is String path of logback.xml.
configurator.doConfigure(is);

另外,我的logback xml如下所示:

<configuration scan="true" scanPeriod="60 seconds">
....
</configuration>

不知何故,它没有在logback.xml中重新扫描我的更改。

<强>疑难解答

因此,我通过添加 debug 属性在logback.xml中启用了调试模式。

<configuration scan="true" scanPeriod="60 seconds" debug="true">
....
</configuration>

当我再次运行应用程序时,我观察到一个日志声明,显示问题的根本原因。

  

12:23:58,462 | -WARN in ch.qos.logback.classic.joran.action.ConfigurationAction - Due   丢失顶级配置文件,更改时重新配置   (配置文件扫描)无法完成。

ConfigurationAction.java类无法在ConfigurationWatchList中找到 mainURL 属性时,会记录此日志。

修改后的配置 - 扫描工作就像一个魅力

所以我改变了通过JoranConfigurator配置回溯的代码。我没有将InputStream作为参数发送到configurator.doConfigure(is),而是使用了重载的 doConfigure 方法,该方法将文件路径本身作为参数。 更新后的代码如下所示:

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
configurator.doConfigure(logConfigPath);// 'logConfigPath' is String path of logback.xml.

更新了调试日志:

  

12:35:37,173 | -INFO in   ch.qos.logback.classic.joran.action.ConfigurationAction - 将扫描   对于[file:/ E:/Samples/config/logback.xml]中的更改   12:35:37173   | ch.qos.logback.classic.joran.action.ConfigurationAction中的-INFO -   将ReconfigureOnChangeTask扫描周期设置为60秒

多数民众赞成!!华友世纪:)

编辑:

查看GenericConfigurator课程时,如果我们使用mainURL方法ConfigurationWatchList doConfigure()URL已注册String }或File作为参数。

该方法的其他三个重载(参数InputStreamInputSourceList<SaxEvent>)不会注册它。

答案 3 :(得分:1)

您的logback.xml似乎是正确的。

这是手册中陈述的行为:http://logback.qos.ch/manual/configuration.html#autoScan

  

鉴于每次调用任何记录器时都会调用ReconfigureOnChangeFilter,而不管记录器级别如何,ReconfigureOnChangeFilter绝对是性能关键。实际上,检查扫描周期是否已经过去,实在是太昂贵了。为了提高性能,ReconfigureOnChangeFilter实际上只在每N次日志记录操作中“活动”一次。根据应用程序记录的频率,可以通过logback动态修改N的值。默认情况下,N为16,但对于CPU密集型应用程序,它可能高达2 ^ 16(= 65536)。

     

简而言之,当配置文件发生更改时,它将自动重新加载,但仅在多次记录器调用之后以及扫描周期确定的延迟之后。

只是尝试记录更多消息,看看配置是否正确加载。

我希望这会帮助你。

致以最诚挚的问候,

米格尔

答案 4 :(得分:0)

对于Logback 1.2.3,如果文件路径包含+,则重新扫描配置文件似乎也无提示地失败。例如。这有效:

-Dlogback.configurationFile=etc/logback.xml

这失败了:

-Dlogback.configurationFile=etc+/logback.xml

Logback可以在初始化时正确加载logback.xml;在后一种情况下,只是重新加载失败。如果+不起作用,则其他字符也可能有问题。

我去提交了一个错误,但是从https://logback.qos.ch/bugreport.html链接的JIRA实例当前似乎无法运行。