【问题标题】:Programmatically Enable Logback in Debug Mode?以编程方式在调试模式下启用 Logback?
【发布时间】:2011-12-12 23:20:32
【问题描述】:

我想知道是否有办法以编程方式启用调试模式。由于我不允许使用配置文件,因此我目前正在以编程方式配置所有内容。最近,我遇到了一个问题,尽管 FileAppender 工作得很好,但 RollingFileAppender 停止写入文件。事实上,RollingFileAppender 上周也在工作,我所知道的一切都没有改变。

如果有办法启用调试,请告诉我,因为使用配置文件 (logback.xml) 似乎不起作用。

【问题讨论】:

    标签: java logging configuration logback rollingfileappender


    【解决方案1】:

    在我发布问题后,Tony 在这里提供了一个很好的答案。

    http://old.nabble.com/Enable-Debugging-Mode-Programmatically--td32961424.html

    当您试图弄清楚为什么 LogBack 在某些情况下不起作用时,这非常有用。我选择在我的初始化代码中使用第一种方式。

    请记住,这是您选择不使用像我的用例中那样的配置文件的时候。


    发件人:tony19

    有几种方法:

    1) 使用 StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext)

    2) 加载配置 XML 的硬编码字符串 w/configuration.debug 设置为“真”:

    static final String LOGBACK_XML = 
        "<configuration debug='true'>" + 
        "  <appender name='FILE' class='ch.qos.logback.core.RollingFileAppender'>" +
        "    <file>foo.log</file>" +
        "    <append>true</append>" +
        "    <encoder>" +
        "      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>" +
        "    </encoder>" +
        "  </appender>" +
        "  <root level='INFO'>" +
        "    <appender-ref ref='FILE' />" +
        "  </root>" +
        "</configuration>"
        ;
    
    static public void configLogback() {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
                try {
               JoranConfigurator configurator = new JoranConfigurator();
               configurator.setContext(lc);
               lc.reset();
    
               configurator.doConfigure(new ByteArrayInputStream(LOGBACK_XML.getBytes()));
           } catch (JoranException je) {
               je.printStackTrace();
           }
    
           // you can also print the errors/warning explicitly (instead of debug='true' in xml)
           //StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多