以编程方式在调试模式下启用Logback?
问题描述:
我想知道是否有方法以编程方式启用调试模式。由于我不允许使用配置文件,因此我目前正在以编程方式配置所有内容。最近,我遇到了一个问题,RollingFileAppender通过FileAppender停止写入文件非常好。实际上,RollingFileAppender上周也在工作,而且我没有意识到自那以后发生了变化。以编程方式在调试模式下启用Logback?
请让我知道是否有办法启用调试,因为使用配置文件(logback.xml)似乎没有工作。
答
Tony在发布问题后提供了一个很好的答案。
http://old.nabble.com/Enable-Debugging-Mode-Programmatically--td32961424.html
这是当你试图找出原因的logback不会在某些情况下工作非常有帮助。我选择使用第一种方式来初始化代码。
请记住,这是当你选择不使用配置文件就像在我的用例。
来源:tony19
有几个方面:
1)使用StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext)
OR
2)负载硬编码的字符串配置XML w/configuration.debug设置为'true':
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);
}