Spring上下文初始化阶段Log4j2日志格式解析错误排查求助
我最近在给一个遗留系统集成Spring框架,目的是让系统结构更清晰,但有个硬性限制:程序的启动流程绝对不能改——因为这套启动逻辑关联了上百个任务,改动的话适配成本太高了。
目前程序是通过这个命令启动的:
java -cp program.jar ProgramMain -class_name "script" ...
我的ProgramMain类的main方法是这么写的:
public static void main(String[] args) { try { setEnvironmentVariables(args); ApplicationContext context = new AnnotationConfigApplicationContext(EnvironmentConfiguration.class); var className = context.getBean("className", String.class); var executionScript = context.getBean(className, ScriptInterface.class); System.out.println("Executing script"); executionScript.setup(); executionScript.run(); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }
功能上一切正常,但日志输出里总是有一堆格式解析错误,而且这些错误是在初始化AnnotationConfigApplicationContext、加载EnvironmentConfiguration之前就出现的。错误日志精简后如下:
2025-02-17T13:38:44.732463534Z main ERROR Unrecognized format specifier [d] 2025-02-17T13:38:44.734027716Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern. 2025-02-17T13:38:44.734713836Z main ERROR Unrecognized format specifier [thread] 2025-02-17T13:38:44.734788519Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern. ... 2025-02-17T13:38:44.742182084Z main ERROR Unrecognized format specifier [msg] 2025-02-17T13:38:44.742221296Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern. 2025-02-17T13:38:44.742265692Z main ERROR Unrecognized format specifier [n] 2025-02-17T13:38:44.742302840Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern. [main] INFO com.package.program.configuration.EnvironmentConfiguration - Setting up environment
最后那行INFO日志是EnvironmentConfiguration构造方法的第一行输出,可见这些ERROR都是Spring上下文初始化早期、还没加载我的配置类时产生的。
我先贴一下当前依赖树里和日志相关的部分:
[INFO] | | \- commons-logging:commons-logging:jar:1.1.3:compile [INFO] +- org.apache.logging.log4j:log4j-api:jar:2.24.3:compile [INFO] +- org.apache.logging.log4j:log4j-core:jar:2.24.3:compile [INFO] \- org.apache.logging.log4j:log4j-slf4j2-impl:jar:2.24.3:compile [INFO] | \- org.slf4j:jcl-over-slf4j:jar:1.7.10:compile [INFO] +- org.slf4j:slf4j-api:jar:2.0.16:compile [INFO] +- org.slf4j:slf4j-simple:jar:2.0.16:compile
我检查过了,没有同类型Jar包多版本冲突的问题。而且系统其他地方用slf4j.Logger打日志都是正常的,比如这种输出就没问题:[main] INFO com.package.program.scripts.AbstractScriptInterfaceImpl - Running TestScript
我试过调整依赖组合,目前找到的“最优”情况是只保留slf4j-api和slf4j-simple这两个依赖,这时只会出现一条错误:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
最后贴一下我的log4j2.xml配置:
<Configuration> <Appenders> <Console name="Console"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
另外补充一下完整依赖树里的其他部分,供大家参考:
[INFO] +- me.tongfei:progressbar:jar:0.10.1:compile [INFO] | \- org.jline:jline-terminal:jar:3.24.1:compile [INFO] | \- org.jline:jline-native:jar:3.24.1:compile [INFO] +- org.springframework:spring-web:jar:6.2.0:compile [INFO] | +- org.springframework:spring-core:jar:6.2.0:compile [INFO] | | \- org.springframework:spring-jcl:jar:6.2.0:compile [INFO] | \- io.micrometer:micrometer-observation:jar:1.14.0:compile [INFO] | \- io.micrometer:micrometer-commons:jar:1.14.0:compile [INFO] +- org.springframework.boot:spring-boot:jar:3.4.0:compile [INFO] +- org.springframework.boot:spring-boot-autoconfigure:jar:3.2.4:compile [INFO] +- org.springframework:spring-context:jar:6.2.0:compile [INFO] | +- org.springframework:spring-aop:jar:6.2.0:compile [INFO] | \- org.springframework:spring-expression:jar:6.2.0:compile [INFO] +- org.springframework:spring-beans:jar:6.2.0:compile [INFO] +- io.minio:minio:jar:8.5.12:compile [INFO] | +- com.carrotsearch.thirdparty:simple-xml-safe:jar:2.7.1:compile [INFO] | +- com.google.guava:guava:jar:33.0.0-jre:compile [INFO] | | +- com.google.guava:failureaccess:jar:1.0.2:compile [INFO] | | +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile [INFO] | | +- com.google.code.findbugs:jsr305:jar:3.0.2:compile [INFO] | | +- org.checkerframework:checker-qual:jar:3.41.0:compile [INFO] | | +- com.google.errorprone:error_prone_annotations:jar:2.23.0:compile [INFO] | | \- com.google.j2objc:j2objc-annotations:jar:2.8:compile [INFO] | +- com.squareup.okhttp3:okhttp:jar:4.12.0:compile [INFO] | | +- com.squareup.okio:okio:jar:3.6.0:compile [INFO] | | | \- com.squareup.okio:okio-jvm:jar:3.6.0:compile [INFO] | | | \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.9.10:compile [INFO] | | \- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.8.21:compile [INFO] | | +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.8.21:compile [INFO] | | \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.8.21:compile [INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.16.1:compile [INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.16.1:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.16.1:compile [INFO] | +- org.bouncycastle:bcprov-jdk18on:jar:1.78:compile [INFO] | +- org.apache.commons:commons-compress:jar:1.26.0:compile [INFO] | +- commons-codec:commons-codec:jar:1.16.1:compile [INFO] | \- org.xerial.snappy:snappy-java:jar:1.1.10.5:compile [INFO] +- org.apache.commons:commons-text:jar:1.12.0:compile [INFO] | \- org.apache.commons:commons-lang3:jar:3.14.0:compile [INFO] +- org.reflections:reflections:jar:0.9.11:compile [INFO] | \- org.javassist:javassist:jar:3.21.0-GA:compile [INFO] +- com.inhouse.elasticsearch:elastic_connector:jar:7.9.1-0.8:compile [INFO] | +- com.fasterxml.jackson.dataformat:jackson-dataformat-properties:jar:2.8.6:compile [INFO] | +- org.elasticsearch.client:elasticsearch-rest-high-level-client:jar:7.9.1:compile [INFO] | | +- org.elasticsearch.plugin:mapper-extras-client:jar:7.9.1:compile [INFO] | | +- org.elasticsearch.plugin:parent-join-client:jar:7.9.1:compile [INFO] | | +- org.elasticsearch.plugin:aggs-matrix-stats-client:jar:7.9.1:compile [INFO] | | +- org.elasticsearch.plugin:rank-eval-client:jar:7.9.1:compile [INFO] | | \- org.elasticsearch.plugin:lang-mustache-client:jar:7.9.1:compile [INFO] | | \- com.github.spullara.mustache.java:compiler:jar:0.9.6:compile [INFO] | +- org.elasticsearch:elasticsearch:jar:7.9.1:compile [INFO] | | +- org.elasticsearch:elasticsearch-core:jar:7.9.1:compile [INFO] | | +- org.elasticsearch:elasticsearch-secure-sm:jar:7.9.1:compile [INFO] | | +- org.elasticsearch:elasticsearch-x-content:jar:7.9.1:compile [INFO] | | | +- org.yaml:snakeyaml:jar:1.26:compile [INFO] | | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.10.4:compile [INFO] | | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.10.4:compile [INFO] | | | \- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.10.4:compile [INFO] | | +- org.elasticsearch:elasticsearch-geo:jar:7.9.1:compile [INFO] | | +- org.apache.lucene:lucene-core:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-analyzers-common:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-backward-codecs:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-grouping:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-highlighter:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-join:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-memory:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-misc:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-queries:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-queryparser:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-sandbox:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-spatial-extras:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-spatial3d:jar:8.6.2:compile [INFO] | | +- org.apache.lucene:lucene-suggest:jar:8.6.2:compile [INFO] | | +- org.elasticsearch:elasticsearch-cli:jar:7.9.1:compile [INFO] | | | \- net.sf.jopt-simple:jopt-simple:jar:5.0.2:compile [INFO] | | +- com.carrotsearch:hppc:jar:0.8.1:compile [INFO] | | +- com.tdunning:t-digest:jar:3.2:compile [INFO] | | +- org.hdrhistogram:HdrHistogram:jar:2.1.9:compile [INFO] | | \- org.elasticsearch:jna:jar:5.5.0:compile [INFO] | +- org.elasticsearch.client:elasticsearch-rest-client:jar:7.9.1:compile [INFO] | | +- org.apache.httpcomponents:httpasyncclient:jar:4.1.4:compile [INFO] | | \- org.apache.httpcomponents:httpcore-nio:jar:4.4.12:compile [INFO] | +- joda-time:joda-time:jar:2.10.10:compile
我实在搞不懂这些日志格式解析错误是从哪来的,调整了好几种依赖组合都没解决,有没有大佬能给点思路?谢谢了!
备注:内容来源于stack exchange,提问作者Fl.pf.

