Logstash logback 编码器因 java.util.ServiceConfigurationError 而失败(jackson.datatype.jsr310.JavaTimeModule 无法实例化)

Logstash logback encoder failing with java.util.ServiceConfigurationError (jackson.datatype.jsr310.JavaTimeModule could not be instantiated)

我正在尝试在我的 springbootapp (2.4.8) 中进行基于 JSON 的日志记录。我已按照 github 和其他一些文档以及 Whosebug 相关问题中提到的说明进行操作。但是,我无法解决问题。

下面是我得到的异常:

net.logstash.logback.encoder.LogstashEncoder@4fcee388 中的错误 - 动态加载 jackson 模块时出错 java.util.ServiceConfigurationError:com.fasterxml.jackson.databind.Module:提供商 com.fasterxml.jackson.datatype .jsr310.JavaTimeModule 无法实例化

我确实确保添加了 logstash-logback-encoder 依赖项并且 logback 依赖项 >= 1.2.0(我使用的是 1.2.3)。有关详细信息,请参阅依赖关系树的摘录。

[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ <my-spring-boot-app> --
[INFO] +- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] +- net.logstash.logback:logstash-logback-encoder:jar:6.6:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] +- ch.qos.logback:logback-access:jar:1.2.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.9.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.3:compile

请看对应的src/main/resources/logback.xml:

<configuration>
    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    </appender>
    <logger name="jsonLogger" additivity="false" level="DEBUG">
        <appender-ref ref="consoleAppender"/>
    </logger>
    <root level="INFO">
        <appender-ref ref="consoleAppender"/>
    </root>
</configuration>

以下是我得到的运行时异常:

   ERROR in net.logstash.logback.encoder.LogstashEncoder@4fcee388 - Error occurred while dynamically loading jackson modules java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.datatype.jsr310.JavaTimeModule could not be instantiated

下面是对应的 pom.xml 依赖文件定义:

<properties>
    <java.version>11</java.version>
    <scribejava.version>5.6.0</scribejava.version>
    <ch.qos.logback.version>1.2.3</ch.qos.logback.version>
</properties>



<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>

<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>6.6</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>${ch.qos.logback.version}</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${ch.qos.logback.version}</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-access</artifactId>
    <version>${ch.qos.logback.version}</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${com.fasterxml.jackson.core.version}</version>
</dependency>

我想我明白了。原来是依赖问题。

(基本上,使用 logstash 6.6,jackson.core 2.9.3 抛出上述错误。当我移动到 2.12.1 时,事情开始工作。下面是正在使用的版本的实际细节 - 从依赖树中摘录)

[INFO] +- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] +- net.logstash.logback:logstash-logback-encoder:jar:6.6:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] +- ch.qos.logback:logback-access:jar:1.2.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.12.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.12.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.1:compile