gpt4 book ai didi

spring-boot - 自定义log4j插件和Spring Boot

转载 作者:行者123 更新时间:2023-12-03 05:34:02 31 4
gpt4 key购买 nike

我有一个要使用的自定义log4j布局插件。在IntelliJ中运行或与./gradlew bootRun一起运行时,插件可以正常工作。不使用./gradlew bootJar构建启动jar。

启用-Dlog4j.debug=true可以发现在IntelliJ中,bootRun使用sun.misc.Launcher $ AppClassLoader,而jar使用org.springframework.boot.loader.LaunchedURLClassLoader。

挖掘日志可以发现以下内容:

INFO StatusLogger Scanning for classes in '/home/rohdef/.m2/repository/dk/rohdef/logging/log4j/0.4.5-SNAPSHOT/log4j-0.4.5-SNAPSHOT.jar' matching criteria annotated with @Plugin
DEBUG StatusLogger Checking to see if class dk.rohdef.logging.log4j.CustomLog4jLayout matches criteria annotated with @Plugin
DEBUG StatusLogger Checking to see if class dk.rohdef.logging.log4j.context.Log4jDiagnosticContext matches criteria annotated with @Plugin
DEBUG StatusLogger Checking to see if class dk.rohdef.logging.log4j.CustomLog4jLayout$1 matches criteria annotated with @Plugin
DEBUG StatusLogger Checking to see if class dk.rohdef.logging.log4j.CustomLog4jLayout$Builder matches criteria annotated with @Plugin
DEBUG StatusLogger Took 0.009241 seconds to load 1 plugins from package dk.enettet.rohdef.log4j
DEBUG StatusLogger PluginManager 'Core' found 118 plugins

而jar记录:
INFO StatusLogger Scanning for classes in '/home/rohdef/git/foo-backend/foo-0.0.1-SNAPSHOT.jar' matching criteria annotated with @Plugin
DEBUG StatusLogger Took 0.158622 seconds to load 0 plugins from package dk.rohdef.logging.log4j
DEBUG StatusLogger PluginManager 'Core' found 117 plugins

符合预期, log4j-0.4.5-SNAPSHOT.jarfoo-0.0.1-SNAPSHOT.jar内部。

这使我怀疑LaunchedURLClassLoader的方式是错误的根源。我说得对吗?如果是这样,这是一个错误还是我使用错了?如何使我的插件正常工作?

最佳答案

来自gitter的Andy Wilkinson提供了答案:

在我看来,Log4j对它在类路径上找到的jar文件的结构进行了假设,而这些假设并不适用于使用带有嵌套在BOOT-INF/lib中的jar的胖jar。

您可以使用requiresUnpack,以便胖子 jar 自动解压缩log4j-0.4.5-SNAPSHOT.jar,以便直接在Log4j应该能够对其进行扫描的文件系统上使用它:https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-configuring-unpacking

关于spring-boot - 自定义log4j插件和Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57606210/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com