zoukankan      html  css  js  c++  java
  • springboot2.+的整合log4j2错误解决浅谈

    前言

    都知道log4j2比系统默认的日志要好,所以我们在整合springboot框架的时候会引入log4j2,但是我们也必须要把框架本来的日志要去掉才行,就像是不是亲生的一样,一个道理。

    不想听废话的直接转到第四步

    第一步

    第一步当然是上网搜索啦,于是你会搜到很多相关的博客,这个时候你可能会找几个看一看,看一下评论和阅读量,然后才决定要不然安装这个来操作,我也是这样的:

    按照网上的要求,做了很多变更,但是最后还是没有配置成功,因为springboot本身的依赖默认是logging,而且springboot默认的组件中很多都依赖了logging。

    首先你要整合。经过你综合比较,你会选择一个你觉得靠得住的,来给你的项目进行操作。这个步骤也简单,一般就三个步骤:

    • pom文件导入依赖log4j2
    • 新建log4j2.xml的配置文件(命名系统默认的是log4j2-spring.xml,不过这个随意的,反正下一步就要配置了)
    • 在springboot的配置文件application中进行配置,指定配置文件

    一般情况下,教程就结束了。

    第二步

    这个时候你会发现你运行没有成功,是什么错误呢?

    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
    SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
    13:55:11.713 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
    13:55:11.721 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
    13:55:11.722 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/D:/CodeSpace/photobuyapi/target/classes/]
    Logging system failed to initialize using configuration from 'classpath:log4j2.xml'
    java.lang.IllegalStateException: Logback configuration error detected: 
    ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:17 - no applicable action for [properties], current ElementPath  is [[configuration][properties]]
    ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:35 - no applicable action for [property], current ElementPath  is [[configuration][properties][property]]
    ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:16 - no applicable action for [appenders], current ElementPath  is [[configuration][appenders]]
    ERROR in ch.qos.logback.core.joran.spi.Interpreter@9:53 - no applicable action for [Console], current ElementPath  is [[configuration][appenders][Console]]
    ERROR in ch.qos.logback.core.joran.spi.Interpreter@10:92 - no applicable action for [PatternLayout], current ElementPath  is [[configuration][appenders][Console][PatternLayout]]

    可能就是这种错误,然后你就疯狂的查看是不是自己哪个步骤不对,然后再到网上搜

    可能就是这个答案:

    https://blog.csdn.net/blueheart20/article/details/80363870

    告诉你要排除依赖,因为springboot本身的依赖中就有logging的依赖,你要去除,

    然后你也去掉了,你可能去掉的是下面这个:

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-access</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    当然你也可能比我少,但是你这个时候是否想到了一个问题,springboot只有这一个依赖是有logging的?

    当然不是,如果你导入的依赖比较多的话,那么你需要逐个检查一遍你的依赖中是否包含logging,怎么检查?

    第三步

    首先要看你的项目现有的依赖:

    这个里面存放的是你现在项目导入的依赖,你需要查看是否有如下几个:

     有的话,说明你的依赖中还是没有把本身(logback)的日志去掉,如何去掉?

    怎么查看有哪些依赖引入了logging?

    第四步

    你的依赖中还是没有把本身(logback)的日志去掉,如何去掉?

    怎么查看有哪些依赖引入了logging?

    查看依赖中是<artifactId>mybatis-spring-boot-starter</artifactId>  或者<artifactId>mybatis-spring-boot-starter  + jdbc或者是其他的

    你按住ctrl +鼠标左键  点进去

    会出现如下页面:

     在下面你会找到这个:

    点进去你会发现是这个:

     你需要做的就是把所有有这种的依赖去掉logging的引入:

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <!--排除默认的日志-->
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-access</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    把红色的加上。

    记住是所有的依赖,比如

    <artifactId>mybatis-spring-boot-starter</artifactId> 这个也有,你要逐个查看,直到项目依赖中没有

     

    如有错误欢迎指证。

     
  • 相关阅读:
    一些用位运算解决的方法
    代码之美中的二分搜索算法Java版
    Connection listeners: accepting TCP connections 翻译
    Android网络编程1
    HelloWorldForU 九度Online Judge
    Ubuntu下设置默认java
    分享一个libevent server——cliserver
    编程原则
    hive迁移hadoop namenode时遇到的问题
    python for语句
  • 原文地址:https://www.cnblogs.com/Anxc/p/12604521.html
Copyright © 2011-2022 走看看