简体   繁体   English

Logback 找不到资源 logback.xml

[英]Logback Could NOT find resource logback.xml

It's working to output to console.它适用于 output 到控制台。 logger.info("Hello world info."); logger.info("你好世界信息。"); //works just fine... //工作得很好...

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

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. Logback尝试在类路径中找到一个名为logback.groovy的文件。

  2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath. 如果找不到此类文件,则logback会尝试在类路径中找到名为logback-test.xml的文件。

  3. If no such file is found, it checks for the file logback.xml in the classpath.. 如果找不到这样的文件,它将在类路径中检查文件logback.xml。

  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会使用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中指定路径,也可以像这样以编程方式进行操作

对我来说解决这个特定问题的一个简短而快速的解决方案是删除该 logback.xml 并 maven 更新项目

"

For me the problem arose due to mistakenly having 2 slf4j-api libraries on the classpath.对我来说,问题是由于错误地在类路径上有 2 个 slf4j-api 库而出现的。 Removing one of them did the trick.删除其中一个就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM