Java Logback 找不到资源 logback.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21831815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:57:43  来源:igfitidea点击:

Logback Could NOT find resource logback.xml

javaxmllogginglogback

提问by M_Tech

It's working to output to console. logger.info("Hello world info."); //works just fine...

它正在输出到控制台。logger.info("Hello world info."); //工作正常...

However the following code returns 'Could NOT find resource' error:

但是,以下代码返回“无法找到资源”错误:

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

I'm using the following XML:

我正在使用以下 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>

And I've copied it into the root of several locations in my classpath (Windows7\Environment Variables\System Variables\Path) but I still get the error 'resource not found'. Any ideas?

我已经将它复制到我的类路径(Windows7\Environment Variables\System Variables\Path)中几个位置的根目录中,但我仍然收到错误“找不到资源”。有任何想法吗?

回答by shabby

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

logback has a default way of finding the configuration file here is how the documentation goes:

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.

让我们从讨论 logback 尝试配置自身所遵循的初始化步骤开始:

  1. Logback 尝试在类路径中找到一个名为 logback.groovy 的文件。

  2. 如果没有找到这样的文件,logback 会尝试在类路径中找到一个名为 logback-test.xml 的文件。

  3. 如果没有找到这样的文件,它会检查类路径中的文件 logback.xml..

  4. 如果两个文件都没有找到,logback 会使用 BasicConfigurator 自动配置自己,这将导致日志输出被定向到控制台。

so probably in your case it is loading the basic configuration when you are seeing the output in console. You can try specifying the path in class-path or do it programatically like this

所以可能在您的情况下,当您在控制台中看到输出时,它正在加载基本配置。您可以尝试在 class-path 中指定路径或以编程方式执行此操作