Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback can't find logback.xml even though it exists (on the classpath)

I've an issue with logback. I set it up (using maven) and everything seems fine except that Logback reports it can't find the configuration file (but I'm able to log to the console using the default logger configuration).

[#|2013-07-03T07:55:30.843+0200|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=124;_ThreadName=Thread-2;|07:54:39,844 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]

07:54:39,844 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]

07:54:39,844 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]

07:54:39,847 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration. |#]

I put the configuration file (called logback.xml) into the src/main/resources folder of my Maven artifact (which is a WAR). Interestingly, if I attempt to load the config from the classpath, I succeed:

Reader r = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("logback.xml"));
StringWriter sw = new StringWriter();
char[] buffer = new char[1024];
for (int n; (n = r.read(buffer)) != -1; )
    sw.write(buffer, 0, n);
String str = sw.toString();
System.out.println(str);

Which prints my sample configuration file:

[#|2013-07-03T07:55:30.844+0200|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=124;_ThreadName=Thread-2;|<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root> </configuration>|#]

My pom.xml has the following entries:

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
        </dependency>
        
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.13</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>

Which is packed as a WAR file (inside an EAR file). The location of the logback.xml inside the WAR file is as follows: WEB-INF/classes/logback.xml

Does anybody have an idea what's wrong with my setup?

Many thanks for your help

stupidSheep

like image 340
stupidSheep Avatar asked Jul 03 '13 06:07

stupidSheep


3 Answers

The location within the WAR file is correct, WEB-INF/classes.

The logback configuration documentation talks about where the logback.xml file can be located within a war, but it doesn't mention anything about an EAR.

Could you please try the information at this link? I am wondering if it needs to be packed into the EAR in a specific way.

  1. Glassfish 3 + ear + logback.xml

(edit: second link removed, didn't work)

like image 200
vikingsteve Avatar answered Oct 06 '22 00:10

vikingsteve


Logback invokes very similar code to the code in your example, i.e.

getClassLoader().getResourceAsStream("logback.xml");

If logback cannot find logback.xml, then it must be that the resource is not visible to the class loader that loaded the logback class. This class loader is most probably different than the class loader that loaded your test code which can find logback.xml.

like image 24
Ceki Avatar answered Oct 05 '22 23:10

Ceki


When you deliver the configuration file of the logging framework within an WAR works everything as expected and without any problems. But if you try this with an EAR something magically happens, the logging framework can’t find the configuration file. And it uses it’s default behavior. I solved it doing the following:

  1. Create a new folder directly under the EAR folder. For example, create a new folder named "classes" --> MyEar/classes

  2. Place your logback.xml file in this new folder: MyEar/classes/logback.xml

  3. In your WAR file's MANIFEST.MF file, add this new folder to the classpath: Manifest-Version: 1.0 Class-Path: classes

like image 28
Hossein Mohammadi Avatar answered Oct 06 '22 00:10

Hossein Mohammadi