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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 02:57:56
vikingsteve

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)

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.

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

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"]
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!