Logback cannot find logback.xml, although it exists (in the classpath)

I have a problem with logback. I configured it (using maven) and everything seems fine, except that the log reports cannot find the configuration file (but I can enter 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 = threaded 2; | 07: 54: 39,844 | -INFO in ch.qos.logback.classic.LoggerContext [default] - Could not find the 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] - default configuration setting. | #]

I put the configuration file (called logback.xml) in the src/main/resources folder of my Maven artifact (this is WAR). Interestingly, if I try to load the configuration from the class path, I will 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); 

What prints my example 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 data:

  <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 packaged as a WAR file (inside an EAR file). The location of the logback.xml file inside the WAR file is as follows: WEB-INF/classes/logback.xml

Does anyone have any idea what happened to my setup?

Many thanks for your help

stupidSheep

+7
java maven war logback
source share
2 answers

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

The backup configuration documentation talks about where the logback.xml file may be located within the war, but it does not say anything about the EAR.

Could you try the information on this link? I am wondering if it needs to be packaged in an EAR in a certain way.

  • Glassfish 3 + ear + logback.xml

(edit: second link deleted, not working)

+5
source share

Logback calls very similar code in your example, i.e. getClassLoader (). getResourceAsStream ("logback.xml"). If logback cannot find logback.xml, then it should be that the resource is not displayed to the class loader loading the log class. This classloader is most likely different from the classloader, which loaded your test code, which can find logback.xml.

+3
source share

All Articles