M_Tech
M_Tech

Reputation: 350

Logback Could NOT find resource logback.xml

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?

Upvotes: 16

Views: 50261

Answers (4)

Pradeep Anchan
Pradeep Anchan

Reputation: 1891

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.

Upvotes: 0

hidden_machine
hidden_machine

Reputation: 191

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

Upvotes: 0

rishi
rishi

Reputation: 1

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

Upvotes: 0

shabby
shabby

Reputation: 3222

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

Upvotes: 8

Related Questions