Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 518
M_Tech Avatar asked Feb 17 '14 14:02

M_Tech


People also ask

Where can I find Logback xml?

A sample file, called logback-template. xml, is provided in the resources subdirectory of the Java SDK installation. The package entered below for logging, com.

How do I use Logback xml in Spring Boot?

To configure Logback for a Spring Boot project via XML, create the logback. xml or logback-spring. xml file under the src/main/resources folder. The configuration in XML file will override the logging properties in the application.


1 Answers

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

like image 80
shabby Avatar answered Sep 16 '22 19:09

shabby