2011-12-12 57 views
1

我想知道是否有方法以編程方式啓用調試模式。由於我不允許使用配置文件,因此我目前正在以編程方式配置所有內容。最近,我遇到了一個問題,RollingFileAppender通過FileAppender停止寫入文件非常好。實際上,RollingFileAppender上週也在工作,而且我沒有意識到自那以後發生了變化。以編程方式在調試模式下啓用Logback?

請讓我知道是否有辦法啓用調試,因爲使用配置文件(logback.xml)似乎沒有工作。

回答

2

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); 
} 
相關問題