How can I determine what log configuration source Logback actually used?

后端 未结 4 1253
半阙折子戏
半阙折子戏 2020-12-24 06:01

log4j has a property, log4j.debug, which will helpfully provide the user with an indication of which configuration file was actually used to configure the loggi

4条回答
  •  悲哀的现实
    2020-12-24 06:29

    If you want to go deep into Logback, you can do the following

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import ch.qos.logback.classic.LoggerContext;
    import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
    
    public class Main {
    
        private static final Logger logger = LoggerFactory.getLogger(Main.class);
    
        public static void main(String[] args) throws Exception {
            LoggerContext loggerContext = ((ch.qos.logback.classic.Logger)logger).getLoggerContext();
            URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
            System.out.println(mainURL);
            // or even
            logger.info("Logback used '{}' as the configuration file.", mainURL);
        }
    }
    

    It will print the URL of the loaded configuration file.

提交回复
热议问题