springboot整合log4j2

  1. 导入依赖

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    
    <!--log4j2-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.17.2</version>
    </dependency>
    

    !!!注意,屏蔽内部日志部分必须添加上,因为其自带的是logback的依赖

    <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>
        </exclusions>
    </dependency>
    
  2. 添加配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Configuration status="WARN">
        <Properties>
            <Property name="patternLayout">%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Property>
            <!--生成日志保存位置-->
            <Property name="logdir">D:/home/demo</Property>
        </Properties>
    
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="${patternLayout}"/>
    
    <!--            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>-->
                <!--控制台将会打印执行sql及结果-->
                <!--<ThresholdFilter level="TRACE"/>-->
                <ThresholdFilter level="DEBUG"/>
            </Console>
    
            <RollingFile name="InfoLogger"
                         fileName="D:/home/demo/debug/log.log"
                         filePattern="D:/home/demo/debug/log_%d{yyyy-MM-dd}.log" append="true">
                <PatternLayout pattern="${patternLayout}"/>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
                <Policies>
                    <!--根据时间来滚动 -->
                    <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                </Policies>
                <!-- 日志保留策略,配置只保留七天 -->
                <DefaultRolloverStrategy>
                    <Delete basePath="${logDir}/debug/" maxDepth="1">
                        <IfFileName glob="log_*.log"/>
                        <IfLastModified age="7d"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
    
            <!-- sql:保存sql相关日志 -->
            <RollingFile name="sql"
                         fileName="${logdir}/sql/sql.log"
                         filePattern="${logdir}/sql/sql_%d{yyyy-MM-dd}.log" append="true">
                <PatternLayout pattern="${patternLayout}"/>
                <Policies>
                    <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                </Policies>
                <DefaultRolloverStrategy>
                    <Delete basePath="${logDir}/sql/" maxDepth="1">
                        <IfFileName glob="sql_*.log"/>
                        <IfLastModified age="7d"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
    
        </Appenders>
    
        <Loggers>
            <!-- 开启mybatis的sql日志, name和mybatis-config.xml的logPrefix一致 . 级别为debug或trace-->
    
            <Logger name="com.example.demo.mybatis.plus.dao" level="TRACE" additivity="false">
                <AppenderRef ref="Console"/>
                <AppenderRef ref="sql"/>
            </Logger>
    
            <Root level="debug">
                <AppenderRef ref="Console"/>
                <AppenderRef ref="InfoLogger"/>
            </Root>
    
        </Loggers>
    
    </Configuration>
    
    

    配置文件名字及位置如下图: log4j2.xml
    在这里插入图片描述

  3. application.yml配置文件

    # 日志配置
    logging:
      config: classpath:log4j2.xml
      level:
      	# 查询接口路径
        com.example.demo.mybatis.plus.dao: DEBUG    
    
    # 如果使用了mybatis-plus就将日志实现
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl  #打印日志
    
    #如果只是使用了mybatis就将日志实现配置文件配置到这个地方
    mybatis:
      configuration:
       log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl #打印日志
    
  4. 常见问题及解决

    ① 常见问题
    问题原因是没有排除spring-boot中自带的日志。

    11:34:08.938 [Thread-1] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@21253577
    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@4:17 - no applicable action for [Properties], current ElementPath  is [[Configuration][Properties]]
    

    在这里插入图片描述
    ② 解决方案
    依赖中添加如上图三所示,屏蔽内置日志。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值