解决springboot启动日志异常问题

问题描述:springboot启动异常,启动后没有日志打印。

 问题原因:slf4j日志实现重复,找不到对应实现类。

问题应对:

1. 是不是项目没起来---->打印的日志数据,到这里就不打印了,在run方法后面加了一行输入print,当启动之后使用debug,能运行到此行,说明正常启动成功的。

2.是不是日志框架冲突导致的--->把日志相关的pom以来都注释,发现print的能打印出来,log.info 相关的打印不出来。

把绑定的两块实现类都找了一下:

[jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/slf4j/slf4j-log4j12/1.7.32/slf4j-log4j12-1.7.32.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

2.1 继续跟下去 slf4j-log4j12 这么抢手,好几处都用到了它

spring-boot-starter-log4j2 依赖的是这个版本log4j-slf4j-impl  

 

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/slf4j/slf4j-log4j12/1.7.32/slf4j-log4j12-1.7.32.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.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 [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.springframework.boot.ApplicationServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.3)

  3.1 问题找到了,解决方案开始实施:

我这里就把对应的日志:排除掉了,目前排除的是这个:

spring-boot-starter-log4j2 依赖的是这个版本log4j-slf4j-impl   ,百度到的消息说这是个桥接包

日志框架:slf4j
日志实现:log4j2
桥接包:log4j-slf4j-impl

桥接包log4j-slf4j-impl起到适配的作用,因为市面上的日志实现互不兼容,日志框架slf4j要想适用于日志实现log4j2,就需要使用桥接包

具体啥情况咱也不太懂,反正我是认为排除一个依赖就该能运行了吧? 当时是在这里

spring-boot-starter-web 进行的依赖排除,我发现并没有排除掉依赖,我就百度搜了一下,然后从springboot的基础上进行排除
     <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>

  依赖排除掉之后发现并没有达到预期的结果,还是这样,除了爆红的几条明晃晃的信息之外,没有其他的消息了,调用输出的日志信息也没有正常显示出来,通过debug也没有异常,我慌了,

然后进行百度,

No appenders could be found for logger (org.springframework.boot.ApplicationServletEnvironment).
这条有效信息进行百度的结果都是spring的方式启动的,然后都是提示让本地加log的properties文件,尝试的加上了,结果还是老样子。 
第二条有效信息进行百度:
Please initialize the log4j system properly.
log4j:WARN No appenders could be found for logger (org.springframework.boot.ApplicationServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

  (54条消息) No appenders could be found for logger_u013412790的专栏-CSDN博客

按照博客的说明,我按照第二条的的方式,把配置加到项目最上方

<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.8.2</version>
        </dependency>

启动之后,发现日志能正常打印出来了,心里别提多高兴了!

接下来,看看是否还需要配置文件,把配置文件删除,发现还能正常使用。到此处问题已经解决。

最后:不光要解决问题,以后遇到类似的问题要知道怎么解决和排查,要知其人并知其所以然。

 找到了官方网站:http://www.slf4j.org/manual.html

日志的实现框架大概有以下几种:那么 slf4j-log4j12是哪个日志框架呢?

 点击进去发现说: http://logging.apache.org/log4j/1.2/index.html

2015年8月5日,伐木服务项目管理委员会宣布Log4j 1.x已结束使用。有关公告的完整文本,请参阅阿帕奇博客。建议 Log4j 1 的用户升级到阿帕奇日志 4j 2

 点进去:http://logging.apache.org/log4j/2.x/index.html

看导入pom是这样导入的

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.14.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
  </dependency>
</dependencies>

  --------------------------------------------------------------------------------------------------------------------------

上面的因为日志找不到而导致的异常,今天我就想把日志集成到

log4j2版本上去,发现了奇怪的一幕,目前还是两个实现类,而且实现的是随机使用一个实现类
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/ch/qos/logback/logback-classic/1.2.4/logback-classic-1.2.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.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]

  配置输出日志:

logging.level.root=INFO
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}[%line] TraceId[%X{PFTID}] - %msg%n

原文地址:https://www.cnblogs.com/q1359720840/p/15517861.html