记录SpringBoot 2.X整合Log4j没有输出INFODEBUG等日志信息解决方案

Posted xfearless

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录SpringBoot 2.X整合Log4j没有输出INFODEBUG等日志信息解决方案相关的知识,希望对你有一定的参考价值。

由于批量更新的时候一直无法定位问题出处,就去服务器定位日志,奈何日志一直无法输出,为了能够更好的定位问题,痛定思痛后逐步排查最终解决问题。如有客官看到此处,请不要盲目对号入座,我的项目环境或许与你有区别所以解决方案不一定适合,此贴只作为工作记录,并对出现相同问题,且项目环境相同的朋友作为借鉴而已,如没有帮到,也请嘴下留情。

首先贴出日志文件:log4j2.yml

# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
  status: info
  monitorInterval: 30
  Properties: # 定义全局变量
    Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
      - Dlog.level.console=info
      - name: log.level.console
        value: debug
      - name: log.path
        value: /data/txv1.0/tx-data/logs
      - name: project.name
        value: tx_data
      - name: log.pattern
        value: "%dyyyy-MM-dd HH:mm:ss.SSS -%5p $PID:- [%15.15t] %-30.30C1. : %m%n"
  Appenders:
    Console:  #输出到控制台
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        pattern: $log.pattern
#   启动日志
    RollingFile:
      - name: ROLLING_FILE
        fileName: $log.path/$project.name.log
        filePattern: "$log.path/historyRunLog/$$date:yyyy-MM/$project.name-%dyyyy-MM-dd-%i.log.gz"
        PatternLayout:
          pattern: $log.pattern
        Filters:
#        一定要先去除不接受的日志级别,然后获取需要接受的日志级别
          ThresholdFilter:
            - level: fatal
              onMatch: DENY
              onMismatch: NEUTRAL
            - level: debug
              onMatch: ACCEPT
              onMismatch: DENY
        Policies:
          TimeBasedTriggeringPolicy:  # 按天分类
            modulate: true
            interval: 1
        DefaultRolloverStrategy:     # 文件最多100个
          max: 100
#   平台日志
      - name: PLATFORM_ROLLING_FILE
        ignoreExceptions: false
        fileName: $log.path/platform/$project.name_platform.log
        filePattern: "$log.path/platform/$$date:yyyy-MM/$project.name-%dyyyy-MM-dd-%i.log.gz"
        PatternLayout:
          pattern: $log.pattern
        Policies:
          TimeBasedTriggeringPolicy:  # 按天分类
            modulate: true
            interval: 1
        DefaultRolloverStrategy:     # 文件最多100个
          max: 100
#   业务日志
      - name: BUSSINESS_ROLLING_FILE
        ignoreExceptions: false
        fileName: $log.path/bussiness/$project.name_bussiness.log
        filePattern: "$log.path/bussiness/$$date:yyyy-MM/$project.name-%dyyyy-MM-dd-%i.log.gz"
        PatternLayout:
          pattern: $log.pattern
        Policies:
          TimeBasedTriggeringPolicy:  # 按天分类
            modulate: true
            interval: 1
        DefaultRolloverStrategy:     # 文件最多100个
          max: 100
#   错误日志
      - name: EXCEPTION_ROLLING_FILE
        ignoreExceptions: false
        fileName: $log.path/exception/$project.name_exception.log
        filePattern: "$log.path/exception/$$date:yyyy-MM/$project.name-%dyyyy-MM-dd-%i.log.gz"
        ThresholdFilter:
          level: error
          onMatch: ACCEPT
          onMismatch: DENY
        PatternLayout:
          pattern: $log.pattern
        Policies:
          TimeBasedTriggeringPolicy:  # 按天分类
            modulate: true
            interval: 1
        DefaultRolloverStrategy:     # 文件最多100个
          max: 100
#   DB 日志
      - name: DB_ROLLING_FILE
        ignoreExceptions: false
        fileName: $log.path/db/$project.name_db.log
        filePattern: "$log.path/db/$$date:yyyy-MM/$project.name-%dyyyy-MM-dd-%i.log.gz"
        PatternLayout:
          pattern: $log.pattern
        Policies:
          TimeBasedTriggeringPolicy:  # 按天分类
            modulate: true
            interval: 1
        DefaultRolloverStrategy:     # 文件最多100个
          max: 100
  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: ROLLING_FILE
        - ref: PLATFORM_ROLLING_FILE
        - ref: BUSSINESS_ROLLING_FILE
        - ref: EXCEPTION_ROLLING_FILE
        - ref: DB_ROLLING_FILE
    Logger:
      - name: rolling
        level: info
        additivity: true
        AppenderRef:
          - ref: CONSOLE
          - ref: ROLLING_FILE
      - name: platform
        level: debug
        additivity: true
        AppenderRef:
          - ref: CONSOLE
          - ref: PLATFORM_ROLLING_FILE
      - name: bussiness
        level: info
        additivity: true
        AppenderRef:
          - ref: CONSOLE
          - ref: BUSSINESS_ROLLING_FILE
      - name: exception
        level: debug
        additivity: true
        AppenderRef:
          - ref: CONSOLE
          - ref: EXCEPTION_ROLLING_FILE
      - name: db
        level: debug
        additivity: true
        AppenderRef:
          - ref: CONSOLE
          - ref: DB_ROLLING_FILE

申明:此文件格式借鉴与网上一位忘记叫什么的朋友的,如有雷同请谅解或许我就是借鉴你的。

贴出原始依赖pom文件(部分,跟日志相关的)

<dependencies>
        <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>
        <!-- 配置 log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>
    
        ..........

        <!-- 云服务依赖包 start -->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>$qiniu-version</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>$aliyun-oss-version</version>
        </dependency>
        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>cos_api</artifactId>
            <version>$qcloud-cos-version</version>
        </dependency>
        <!-- 云服务依赖包 end -->
</dependencies>

由于启动之后再服务器中一直存在这样一种警告

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/data/txv1.0/tx-data/webapps/tx-data/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/data/txv1.0/tx-data/webapps/tx-data/WEB-INF/lib/log4j-slf4j-impl-2.11.2.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]

经过多次排查发现这个警告是提示slf4j-log4j12-xxx.jar 的依赖冲突

然后通过pom文件排查是否存在logger相关slf4j-log4j12-xxx.jar包的冲突,如图

技术图片

上图是我去掉依赖冲突所以看不见相关依赖包,如果存在依赖的就会显示被那些jar所依赖,最终发现依赖的jar包

<dependency>
    <groupId>com.qiniu</groupId>
    <artifactId>qiniu-java-sdk</artifactId>
    <version>$qiniu-version</version>
</dependency>

最后去掉这个包所依赖的slf4j-log4j12-xxx.jar包,就解决了如法输出日志信息的问题。

<dependency>
    <groupId>com.qcloud</groupId>
    <artifactId>cos_api</artifactId>
    <version>$qcloud-cos-version</version>
    <exclusions>
        <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </exclusion>
    </exclusions>
</dependency>    

 

以上是关于记录SpringBoot 2.X整合Log4j没有输出INFODEBUG等日志信息解决方案的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2.x 实践记:log4j2.xml

Spring Boot 2.x 实践记:log4j2.xml

Spring Boot 2.x 实践记:log4j2.xml

springboot mybatis 整合后怎么答应输出sql语句

SpringBoot整合Redis并完成工具类

SpringBoot整合日志log4j