2014-02-17 173 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