spring boot配置log4j2日志问题

第一步:配置log4j2.xml resources目录下新建该文件

<?xml version="1.0" encoding="utf-8"?>
<!--日志级别:TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出-->
<!--
    status : 这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,会看到log4j2内部各种详细输出
    monitorInterval : Log4j能够自动检测修改配置文件和重新配置本身, 设置间隔秒数。此处表示每隔300秒重读一次配置文件
-->
<Configuration status="info" monitorInterval="300">
    <!--<properties>-->
    <!--<property name="LOG_HOME">F:\logs</property>-->
    <!--<property name="ERROR_LOG_FILE_NAME">error</property>-->
    <!--</properties>-->
    <Properties>
        <Property name="log_path">logs</Property><!-- 这里的logs为项目根目录下的logs文件夹 -->
        <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        <!-- 高亮控制台输出  -->
        <Property name="log_pattern_highlight">%highlight{${log_pattern}}{FATAL=Bright Red, ERROR=Magenta, WARN=Cyan, INFO=Green, DEBUG=Yellow, TRACE=Bright Blue}</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT"> <!-- 定义类型为Console的Appender -->
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        </Console>
    </Appenders>
    <!--定义logger,只有定义了logger并引入上面的appender,appender才会生效-->
    <Loggers>
        <Root level="info"> <!-- 定义Root Logger -->
            <AppenderRef ref="Console"/> <!-- Root Logger的Appender引用上面定义的Console -->
        </Root>
        <!--定义名字为MainLogger的Logger,其日志级别为info,info以下级别的信息将不会输出 -->
        <Logger name="MainLogger" level="info" additivity="false">
            <AppenderRef ref="Console"/> <!-- Root Logger的Appender引用上面定义的Console -->
        </Logger>
    </Loggers>
</Configuration>

以上配置打印的日志会带行号连接点击连接会跳转到具体行

第二部: 引入依赖,pom.xml中添加


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

第三步:排除依赖,使用exclusion标签排除Logback,具体代码如下

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!--排除spring boot 默认日志-->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

第四步 添加log4j2配置


spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf-8
    username: root
    password: ****
  jpa:
    hibernate:
      ddl-auto: update
      dialect: mysql
    show-sql: true

  logging:
#    config: logback.xml   
    config: log4j2.xml
    level:
      com.mb: debug

spring:
空格logging:
空格空格config: log4j2.xml
最终效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值