【问题标题】:Multiple Slf4j bindings unable to add log4j2 spring boot多个Slf4j绑定无法添加log4j2 spring boot
【发布时间】:2023-02-22 01:43:54
【问题描述】:

在我的项目中有多个 slf4j 绑定

[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:3.0.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:3.0.2:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.9.19:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:3.0.2:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:6.0.4:compile
[INFO] |  +- org.hibernate.orm:hibernate-core:jar:6.1.6.Final:compile
[INFO] |  |  +- jakarta.persistence:jakarta.persistence-api:jar:3.1.0:compile
[INFO] |  |  +- jakarta.transaction:jakarta.transaction-api:jar:2.0.1:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.5.0.Final:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:6.0.2.Final:runtime
[INFO] |  |  +- org.jboss:jandex:jar:2.4.2.Final:runtime
[INFO] |  |  +- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.12.22:runtime
[INFO] |  |  +- org.glassfish.jaxb:jaxb-runtime:jar:4.0.1:runtime
[INFO] |  |  |  \- org.glassfish.jaxb:jaxb-core:jar:4.0.1:runtime
[INFO] |  |  |     +- org.eclipse.angus:angus-activation:jar:1.0.0:runtime
[INFO] |  |  |     +- org.glassfish.jaxb:txw2:jar:4.0.1:runtime
[INFO] |  |  |     \- com.sun.istack:istack-commons-runtime:jar:4.1.1:runtime
[INFO] |  |  +- jakarta.inject:jakarta.inject-api:jar:2.0.0:runtime
[INFO] |  |  \- org.antlr:antlr4-runtime:jar:4.10.1:runtime
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:3.0.1:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:3.0.1:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:6.0.4:compile
[INFO] |  |  +- org.springframework:spring-context:jar:6.0.4:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:6.0.4:compile
[INFO] |  |  \- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
[INFO] |  \- org.springframework:spring-aspects:jar:6.0.4:compile
[INFO] +- com.zaxxer:HikariCP:jar:5.0.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:2.0.6:compile
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:3.0.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:3.0.2:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:3.0.2:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:3.0.2:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:3.0.2:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.4.5:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.4.5:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.19.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.19.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:2.0.6:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.33:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.5:compile
[INFO] |  \- org.hibernate.validator:hibernate-validator:jar:8.0.0.Final:compile
[INFO] |     \- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile
[INFO] +- software.amazon.awssdk:secretsmanager:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:aws-json-protocol:jar:2.18.6:compile
[INFO] |  |  +- software.amazon.awssdk:third-party-jackson-core:jar:2.18.6:compile
[INFO] |  |  \- software.amazon.awssdk:json-utils:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:protocol-core:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:sdk-core:jar:2.18.6:compile
[INFO] |  |  +- software.amazon.awssdk:endpoints-spi:jar:2.18.6:compile
[INFO] |  |  +- software.amazon.awssdk:profiles:jar:2.18.6:compile
[INFO] |  |  \- org.reactivestreams:reactive-streams:jar:1.0.4:compile
[INFO] |  +- software.amazon.awssdk:auth:jar:2.18.6:compile
[INFO] |  |  \- software.amazon.eventstream:eventstream:jar:1.0.1:compile
[INFO] |  +- software.amazon.awssdk:http-client-spi:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:regions:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:annotations:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:utils:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:aws-core:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:metrics-spi:jar:2.18.6:compile
[INFO] |  +- software.amazon.awssdk:apache-client:jar:2.18.6:runtime
[INFO] |  |  +- org.apache.httpcomponents:httpclient:jar:4.5.14:runtime
[INFO] |  |  |  \- commons-codec:commons-codec:jar:1.15:runtime
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.16:runtime
[INFO] |  \- software.amazon.awssdk:netty-nio-client:jar:2.18.6:runtime
[INFO] |     +- io.netty:netty-codec-http:jar:4.1.87.Final:compile
[INFO] |     +- io.netty:netty-codec-http2:jar:4.1.87.Final:compile
[INFO] |     +- io.netty:netty-codec:jar:4.1.87.Final:compile
[INFO] |     +- io.netty:netty-buffer:jar:4.1.87.Final:compile
[INFO] |     \- io.netty:netty-transport-classes-epoll:jar:4.1.87.Final:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.0.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:3.0.2:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.14.1:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.14.1:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.14.1:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.14.1:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.14.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.0.2:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.5:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.5:compile
[INFO] |  +- org.springframework:spring-web:jar:6.0.4:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:6.0.4:compile
[INFO] |     \- org.springframework:spring-expression:jar:6.0.4:compile
[INFO] +- org.springframework.boot:spring-boot-configuration-processor:jar:3.0.2:compile
[INFO] +- org.postgresql:postgresql:jar:42.5.1:runtime
[INFO] |  \- org.checkerframework:checker-qual:jar:3.5.0:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:3.0.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:3.0.2:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:3.0.2:compile
[INFO] |  +- io.micrometer:micrometer-observation:jar:1.10.3:compile
[INFO] |  |  \- io.micrometer:micrometer-commons:jar:1.10.3:compile
[INFO] |  \- io.micrometer:micrometer-core:jar:1.10.3:compile
[INFO] |     +- org.hdrhistogram:HdrHistogram:jar:2.1.12:runtime
[INFO] |     \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime
[INFO] +- org.projectlombok:lombok:jar:1.18.24:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:3.0.2:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:3.0.2:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.0.2:test
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.7.0:test
[INFO] |  |  \- net.minidev:json-smart:jar:2.4.8:test
[INFO] |  |     \- net.minidev:accessors-smart:jar:2.4.8:test
[INFO] |  |        \- org.ow2.asm:asm:jar:9.1:test
[INFO] |  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.0:compile
[INFO] |  |  \- jakarta.activation:jakarta.activation-api:jar:2.1.1:compile
[INFO] |  +- org.assertj:assertj-core:jar:3.23.1:test
[INFO] |  +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] |  +- org.junit.jupiter:junit-jupiter:jar:5.9.2:test
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.9.2:test
[INFO] |  |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] |  |  |  +- org.junit.platform:junit-platform-commons:jar:1.9.2:test
[INFO] |  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.9.2:test
[INFO] |  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.9.2:test
[INFO] |  |     \- org.junit.platform:junit-platform-engine:jar:1.9.2:test
[INFO] |  +- org.mockito:mockito-core:jar:4.8.1:test
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.12.22:test
[INFO] |  |  \- org.objenesis:objenesis:jar:3.2:test
[INFO] |  +- org.mockito:mockito-junit-jupiter:jar:4.8.1:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.1:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-core:jar:6.0.4:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:6.0.4:compile
[INFO] |  +- org.springframework:spring-test:jar:6.0.4:test
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.9.1:test
[INFO] +- com.google.code.gson:gson:jar:2.9.1:compile
[INFO] +- com.internetitem:logback-elasticsearch-appender:jar:1.6:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.14.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-amqp:jar:3.0.2:compile
[INFO] |  +- org.springframework:spring-messaging:jar:6.0.4:compile
[INFO] |  \- org.springframework.amqp:spring-rabbit:jar:3.0.1:compile
[INFO] |     +- org.springframework.amqp:spring-amqp:jar:3.0.1:compile
[INFO] |     |  \- org.springframework.retry:spring-retry:jar:2.0.0:compile
[INFO] |     \- com.rabbitmq:amqp-client:jar:5.16.0:compile
[INFO] +- org.springframework.boot:spring-boot-starter-cache:jar:3.0.2:compile
[INFO] |  \- org.springframework:spring-context-support:jar:6.0.4:compile
[INFO] +- org.springframework.data:spring-data-redis:jar:3.0.1:compile
[INFO] |  +- org.springframework.data:spring-data-keyvalue:jar:3.0.1:compile
[INFO] |  +- org.springframework:spring-tx:jar:6.0.4:compile
[INFO] |  +- org.springframework:spring-oxm:jar:6.0.4:compile
[INFO] |  \- org.springframework:spring-aop:jar:6.0.4:compile
[INFO] +- org.json:json:jar:20220924:compile
[INFO] +- org.apache.commons:commons-pool2:jar:2.11.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:3.0.2:compile
[INFO] |  +- org.mongodb:mongodb-driver-sync:jar:4.8.2:compile
[INFO] |  |  +- org.mongodb:bson:jar:4.8.2:compile
[INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:4.8.2:compile
[INFO] |  |     \- org.mongodb:bson-record-codec:jar:4.8.2:runtime
[INFO] |  \- org.springframework.data:spring-data-mongodb:jar:4.0.1:compile
[INFO] +- org.springframework.mobile:spring-mobile-device:jar:2.0.0.M3:compile
[INFO] +- io.github.resilience4j:resilience4j-spring-boot2:jar:2.0.2:compile
[INFO] |  +- io.github.resilience4j:resilience4j-spring:jar:2.0.2:compile
[INFO] |  |  +- io.github.resilience4j:resilience4j-annotations:jar:2.0.2:compile
[INFO] |  |  +- io.github.resilience4j:resilience4j-consumer:jar:2.0.2:compile
[INFO] |  |  |  +- io.github.resilience4j:resilience4j-core:jar:2.0.2:compile
[INFO] |  |  |  \- io.github.resilience4j:resilience4j-circularbuffer:jar:2.0.2:runtime
[INFO] |  |  \- io.github.resilience4j:resilience4j-framework-common:jar:2.0.2:compile
[INFO] |  \- io.github.resilience4j:resilience4j-micrometer:jar:2.0.2:runtime
[INFO] |     +- io.github.resilience4j:resilience4j-bulkhead:jar:2.0.2:compile
[INFO] |     +- io.github.resilience4j:resilience4j-circuitbreaker:jar:2.0.2:compile
[INFO] |     +- io.github.resilience4j:resilience4j-retry:jar:2.0.2:compile
[INFO] |     +- io.github.resilience4j:resilience4j-ratelimiter:jar:2.0.2:compile
[INFO] |     \- io.github.resilience4j:resilience4j-timelimiter:jar:2.0.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:3.0.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:3.0.2:compile
[INFO] |  |  \- io.projectreactor.netty:reactor-netty-http:jar:1.1.2:compile
[INFO] |  |     +- io.netty:netty-resolver-dns:jar:4.1.87.Final:compile
[INFO] |  |     |  \- io.netty:netty-codec-dns:jar:4.1.87.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns-native-macos:jar:osx-x86_64:4.1.87.Final:compile
[INFO] |  |     |  \- io.netty:netty-resolver-dns-classes-macos:jar:4.1.87.Final:compile
[INFO] |  |     +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.87.Final:compile
[INFO] |  |     \- io.projectreactor.netty:reactor-netty-core:jar:1.1.2:compile
[INFO] |  |        \- io.netty:netty-handler-proxy:jar:4.1.87.Final:compile
[INFO] |  |           \- io.netty:netty-codec-socks:jar:4.1.87.Final:compile
[INFO] |  \- org.springframework:spring-webflux:jar:6.0.4:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] +- com.bucket4j:bucket4j-core:jar:8.1.1:compile
[INFO] +- com.bucket4j:bucket4j-redis:jar:8.0.1:compile
[INFO] +- io.lettuce:lettuce-core:jar:6.2.2.RELEASE:compile
[INFO] |  +- io.netty:netty-common:jar:4.1.87.Final:compile
[INFO] |  +- io.netty:netty-handler:jar:4.1.87.Final:compile
[INFO] |  |  +- io.netty:netty-resolver:jar:4.1.87.Final:compile
[INFO] |  |  \- io.netty:netty-transport-native-unix-common:jar:4.1.87.Final:compile
[INFO] |  +- io.netty:netty-transport:jar:4.1.87.Final:compile
[INFO] |  \- io.projectreactor:reactor-core:jar:3.5.2:compile
[INFO] +- org.springdoc:springdoc-openapi-starter-webmvc-ui:jar:2.0.2:compile
[INFO] |  +- org.springdoc:springdoc-openapi-starter-webmvc-api:jar:2.0.2:compile
[INFO] |  |  \- org.springdoc:springdoc-openapi-starter-common:jar:2.0.2:compile
[INFO] |  |     \- io.swagger.core.v3:swagger-core-jakarta:jar:2.2.7:compile
[INFO] |  |        +- io.swagger.core.v3:swagger-annotations-jakarta:jar:2.2.7:compile
[INFO] |  |        +- io.swagger.core.v3:swagger-models-jakarta:jar:2.2.7:compile
[INFO] |  |        \- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.14.1:compile
[INFO] |  +- org.webjars:swagger-ui:jar:4.15.5:compile
[INFO] |  +- org.webjars:webjars-locator-core:jar:0.52:compile
[INFO] |  \- io.github.classgraph:classgraph:jar:4.8.149:compile
[INFO] \- com.slack.api:slack-api-client:jar:1.27.3:compile
[INFO]    +- com.slack.api:slack-api-model:jar:1.27.3:compile
[INFO]    \- com.squareup.okhttp3:okhttp:jar:4.10.0:compile
[INFO]       +- com.squareup.okio:okio-jvm:jar:3.0.0:compile
[INFO]       |  +- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.7.22:compile
[INFO]       |  |  \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.7.22:compile
[INFO]       |  \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.7.22:compile
[INFO]       \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.7.22:compile
[INFO]          \- org.jetbrains:annotations:jar:13.0:compile

我尝试添加 log4j2 以便我可以配置日志记录模式和 ELK 转发。 但我无法那样做。

这是我实施的

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

尝试添加 log4j2.propertieslog4j2.xml 它们都不起作用。

你能建议任何方法来实现这一目标吗?

谢谢

【问题讨论】:

  • 试过了不工作logback-spring.xml
  • 您将依赖项放在哪里:project/dependenciesproject/dependencyManagement/dependencies?您的依赖关系树没有考虑到它们。

标签: java spring spring-boot maven log4j2


【解决方案1】:

对于日志记录配置,请尝试application.properties => logging.config = classpath:log4j2.xml

对于 ELK 日志记录模式,您尝试过 Slf4J + LogBack 吗? (Slf4J+LogBack 是默认的 Spring 设置)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
   <groupId>net.logstash.logback</groupId>
   <artifactId>logstash-logback-encoder</artifactId>
</dependency>

配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="fase">
    <springProperty scope="context" name="appRootLogLevel" source="logging.level" defaultValue="TRACE" />
    <springProperty scope="context" name="appName" source="spring.application.name" defaultValue="-" />
    <springProperty scope="context" name="componentName" source="spring.application.component-name" defaultValue="-" />
    <springProperty scope="context" name="envName" source="spring.application.env" defaultValue="-" />
    <springProperty scope="context" name="subEnvName" source="spring.application.sub-env-name" defaultValue="-" />
    <springProperty scope="context" name="version" source="info.app.version" defaultValue="No Version"/>

    <appender name="jsonSTDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <logstashMarkers />
                <pattern>
                    <pattern>
                        {
                            "@t": "%date{yyyy-MM-dd'T'HH:mm:ss.SSSZ}",
                            "@s": "%level",
                            "@m": "%message",
                            "@e": "${envName}",
                            "@a": "${appName}",
                            "@cp": "${componentName}",
                            "@scp": "%logger{40}",
                            "@co": "%mdc{correlationId}",
                            "@v": "${version}",
                            "@st": "%exception{full}",
                            "@d": "#asJson{%mdc{dataObject}}"
                        }
                    </pattern>
                </pattern>
                <stackTrace>
                    <fieldName>@st</fieldName>
                    <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                        <maxDepthPerThrowable>30</maxDepthPerThrowable>
                        <maxLength>4096</maxLength>
                        <shortenedClassNameLength>20</shortenedClassNameLength>
                        <exclude>^sun.reflect..*.invoke</exclude>
                        <exclude>^net.sf.cglib.proxy.MethodProxy.invoke</exclude>
                        <rootCauseFirst>true</rootCauseFirst>
                    </throwableConverter>
                </stackTrace>
            </providers>
        </encoder>
    </appender>
    <root level="${appRootLogLevel}">
        <appender-ref ref="jsonSTDOUT" />
    </root>
</configuration>

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 2015-04-24
    • 2015-05-28
    • 2017-05-18
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2018-07-10
    • 2021-05-30
    相关资源
    最近更新 更多