2014-02-17 170 views
4

它正在輸出到控制檯。 logger.info(「Hello world info。」); //但是下面的代碼返回 '找不到資源' 的作品就好了... ...Logback找不到資源logback.xml

錯誤:

Logger logger = LoggerFactory.getLogger("framework.Utilities._Test"); 
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
StatusPrinter.print(lc); 

我使用下面的XML:

<configuration> 
<appender name="FILE" class="ch.qos.logback.core.FileAppender"> 
    <file>C:\Reports\logBack.log</file> 
    <!-- encoders are assigned by default the type 
     ch.qos.logback.classic.encoder.PatternLayoutEncoder --> 
    <encoder> 
     <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern> 
    </encoder> 
</appender> 

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder> 
     <pattern>%msg%n</pattern> 
    </encoder> 
</appender> 

<root level="debug"> 
    <appender-ref ref="FILE" /> 
    <appender-ref ref="STDOUT" /> 
</root> 
</configuration> 

而且我已經將它複製到我的類路徑中的幾個位置的根目錄(Windows7 \環境變量\系統變量\路徑),但我仍然收到錯誤'找不到資源'。有任何想法嗎?

+1

您是如何運行代碼的?您可以通過替換\ by/for文件路徑來嘗試。有時它不起作用 –

回答

4
And I've copied it into the root of several locations in my classpath 

的logback有這裏找到的配置文件的默認方式的文檔怎麼一回事:

Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  1. Logback tries to find a file called logback.groovy in the classpath.

  2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.

  3. If no such file is found, it checks for the file logback.xml in the classpath..

  4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

所以也許在你的情況下,它加載,當你看到在控制檯輸出的基本配置。您可以嘗試在類路徑中指定路徑,或以編程方式執行,如this