以编程方式在调试模式下启用回退? [英] Programmatically Enable Logback in Debug Mode?

查看:342
本文介绍了以编程方式在调试模式下启用回退?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法来以编程方式启用调试模式。因为我不允许使用配置文件,我目前配置所有的程序。最近,我遇到了一个问题,RollingFileAppender停止写入文件,虽然FileAppender工作完全正常。事实上,RollingFileAppender也在上周工作,我没有什么,我知道有改变,因为。

I was wondering if there is a way to enable debug mode programmatically. Since I'm not allowed to use configuration file, I'm currently configure everything programmatically. Recently, I ran into an issue where RollingFileAppender stop writing to file though FileAppender works perfectly fine. In fact, RollingFileAppender was also working last week and nothing that I'm aware of has changed since.

请让我知道如果有一个方法,它使用配置文件(logback.xml)似乎不工作。

Please let me know if there is a way to enable debug since doing it using configuration file (logback.xml) doesn't seem to be working.

推荐答案

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

Tony has provided an excellent answer here after I posted the question.

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

这是非常有用的你试图找出为什么LogBack在某些情况下不工作。我选择了使用第一种方式为我的初始化代码。

This is very helpful when you are trying to figure out why LogBack doesn't work in certain cases. I opted to use the first way for my initialization code.

请记住,这是当你选择不使用配置文件像我的种类的用例。

Keep in mind that this is when you choose not to use a config file like in my kind of use case.

来自:tony19

1)使用StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext)

1) Use StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext)

2)将配置的硬编码字符串XML w / configuration.debug设置为true:

2) Load hard-coded string of configuration XML w/configuration.debug set to '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);
}

这篇关于以编程方式在调试模式下启用回退?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆