14

It's working to output to console. logger.info("Hello world 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:

<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?

M_Tech
  • 330
  • 2
  • 7
  • 16

4 Answers4

9
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:

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.

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

Community
  • 1
  • 1
shabby
  • 3,002
  • 3
  • 39
  • 59
  • If groovy gets checked first why does my log say... 11:29:34,847 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 11:29:34,847 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] Souldn't that order be reversed? – JGleason Feb 14 '19 at 17:34
  • 1
    @JGleason the order in which they are written might not necessarily depict in what they are executed https://stackoverflow.com/a/4959696/40570 – shabby Feb 19 '19 at 08:03
  • @JGleason the order is correct with `logback-test.xml` first. So in a test execution scenario, where both are likely in the classpath, the test setup is preferred. in production the test stuff should not be present and the `logback.xml` will be preferred instead. – Andreas Covidiot Apr 03 '20 at 13:33
0

One short and quick solution that works for me for this particular problem is to delete that logback.xml and maven update the project

rishi
  • 1
0

For me the problem arose due to mistakenly having 2 slf4j-api libraries on the classpath. Removing one of them did the trick.

0

For me, the problem got resolved when i restarted my IDE (Intellij-Idea).
It somehow had cached the java-util-logging library and no amount of tweaking was fixing the issue.
Finally the restart did the trick.

Pradeep Anchan
  • 1,469
  • 1
  • 11
  • 11