Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 7540|回复: 0

【记录】SpringBoot 2.X整合Log4j没有输出INFO、DEBUG等日志信息解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-4-7 12:16:46 | 显示全部楼层 |阅读模式

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

    首先贴出日志文件: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: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %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}-%d{yyyy-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}-%d{yyyy-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}-%d{yyyy-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}-%d{yyyy-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}-%d{yyyy-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>    

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-12-22 18:40 , Processed in 0.054554 second(s), 28 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表