Logback can't find logback.xml even though it

2019-04-24 11:58发布

问题:

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

回答1:

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)



回答2:

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.



回答3:

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



回答4:

So I had similar problem where I had logback.xml in classpath but wasn't being included in the build process. I recently switched over to gradle. I was having issues initially with my resource files not included in the build even though I specifically added src/main/resources to sourceSet of build.gradle.

So my solution at the time was to put the types of files in the include:

includes = ["**/*.css", "**/*.wav", "**/*.mp3", "**/*.mp4", "**/*.png"]

Sometime passed and I noticed my logging config wasn't being applied. I spent a great deal of time tweaking the log and looking up the problem. I soon realized that the file wasn't being included.

String URL = "logback.xml";
System.out.println(ClassLoader.getSystemResource(URL));

I remembered I had to put the type of files in the include. I added the xml type and it worked.

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/java", "src/main/resources"]
            includes = ["**/*.css", "**/*.wav", "**/*.mp3", "**/*.mp4", "**/*.png", "**/*.xml"]
        }
    }
}