大家好!
我在使用 lein 创建 jar 文件时遇到了一个问题,我在项目中添加了一个新的 log4j-layout-template-json,并使用 org.clojure/tools.logging 添加了一个 JSON 模板进行日志记录。当我本地运行时一切正常,但当尝试从 jar 文件运行项目时,我不断出现这个错误
ERROR StatusConsoleListener Unable to locate plugin type for JsonTemplateLayout
ERROR StatusConsoleListener Unable to locate plugin for JsonTemplateLayout
ERROR StatusConsoleListener Could not create plugin of type class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console: java.lang.NullPointerException: Cannot invoke "org.apache.logging.log4j.core.config.plugins.util.PluginType.getElementName()" because "childType" is null
java.lang.NullPointerException: Cannot invoke "org.apache.logging.log4j.core.config.plugins.util.PluginType.getElementName()" because "childType" is null
at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.findNamedNode(PluginElementVisitor.java:104)
所以项目看不到 log4j-layout-template-json,我能够通过替换 /META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat 为插件本身的文件来制作一个可工作的 jar 文件,所以看起来这个文件被覆盖了,lein 不能理解这种 Java 插件模式
所以问题是这里是否有方法可以用 Lein 自动化这个过程,或者转到依赖关系可以解决这个问题,谢谢!
project.clj 文件
(defproject aa "aa"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.11.3"]
[org.clojure/tools.logging "1.2.4"]
[org.clojure/tools.cli "0.3.1"]
[org.clojure/core.async "1.3.610"]
[clj-commons/clj-yaml "1.0.26"]
[com.google.guava/guava "31.1-jre"]
[io.netty/netty-all "4.1.107.Final"]
[org.clojure/core.match "1.0.1"]
[com.github.ben-manes.caffeine/caffeine "3.1.8"]
;;;; Logging
[org.apache.logging.log4j/log4j-slf4j-impl "2.20.0"] ;; libraries using SLF4J won't complain
[org.apache.logging.log4j/log4j-api "2.20.0"]
[org.apache.logging.log4j/log4j-core "2.20.0"]
[org.apache.logging.log4j/log4j-layout-template-json "2.20.0"]]
:jvm-opts ["-Dlog4j2.configurationFile=resources/log4j2.properties"
"-Dlog4j2.garbagefreeThreadContextMap=true"
"-Dclojure.tools.logging.factory=clojure.tools.logging.impl/log4j2-factory"]
:test-selectors {:default (complement :integration)
:integration :integration}
:main ^:skip-aot aa.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})
使用此 https://github.com/arctype-co/log4j2-plugins-cache 插件现在解决了问题,但我想知道如果没有包括新插件是否有解决方案。