Logback scan not working

后端 未结 5 1268
清歌不尽
清歌不尽 2021-02-20 03:18

I am having trouble getting the auto scan functionality of logback to work. It doesn\'t seem to pick up the changes. I have added debug=\"true\" to section and reading it\'s ou

5条回答
  •  粉色の甜心
    2021-02-20 04:04

    I faced similar issue and the root cause turned out to be the way how I was initializing the logback.

    Initial Configuration - Not working:

    Below is the code which I used to configure logback using 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);
    

    In addition, my logback xml looked as below:

    
    ....
    
    

    Somehow, it was not re-scanning my changes in logback.xml.

    Troubleshooting

    So, I enabled debug mode in logback.xml by adding debug attribute as below.

    
    ....
    
    

    When I ran the application again, I observed a log statement showing the root cause of the issue.

    12:23:58,462 |-WARN in ch.qos.logback.classic.joran.action.ConfigurationAction - Due to missing top level configuration file, reconfiguration on change (configuration file scanning) cannot be done.

    This log is being logged by ConfigurationAction.java class when it is not able to find mainURL property in ConfigurationWatchList.

    Modified configuration - Scan working like a charm

    So I changed the code where I was configuring logback via JoranConfigurator. Instead of sending InputStream as parameter to configurator.doConfigure(is) I used the overloaded doConfigure method which takes file path itself as parameter. Updated code looked like this:

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

    Updated debug log:

    12:35:37,173 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/E:/Samples/config/logback.xml]
    12:35:37,173 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 60 seconds

    Thats it!! Hurray :)

    EDIT :

    Looking at GenericConfigurator class, it turns out that, mainURL is registered with ConfigurationWatchList if we use doConfigure() method which takes URL, String or File as parameter.

    Other three overloads of the method (with parameters InputStream, InputSource or List) do not register it.

提交回复
热议问题