目录

在springboot项目中引入log4j-2.x

在springboot项目中引入log4j 2.x

步骤 1:排除 Spring Boot 默认的日志依赖

Spring Boot 默认使用 Logback 作为日志框架,所以需要先排除它,在 pom.xml (如果是 Maven 项目) 中添加如下配置:

<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:添加 Log4j 2.x 依赖

pom.xml 中添加 Log4j 2.x 的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
步骤 3:配置 Log4j 2.x

src/main/resources 目录下创建 log4j2.xml 文件,以下是一个简单的配置示例:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n"/>
        </Console>
        <File name="File" fileName="app.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

这个配置将日志同时输出到控制台和 app.log 文件中。

步骤 4:使用日志

在 Java 代码中使用日志,示例如下:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class YourApplication implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(YourApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("This is an info log message.");
        logger.error("This is an error log message.");
    }
}

在其他类使用需要引入,例如在controller使用

    
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/leeYqsbBasic")
public class LeeYqsbBasicController {
    private static final Logger logger = LoggerFactory.getLogger(LeeSxsDjController.class);
    @PostMapping("/getByCondition2")
    public Result getByCondition2(@RequestBody LeeYqsbBasicVo leeYqsbBasicVo) {
        logger.info("接收到请求,leeYqsbBasicVo = {}", leeYqsbBasicVo.toString());
    }
}

自定义日志格式

https://i-blog.csdnimg.cn/direct/644b248fc4d94d8cb8ada1bb6fa3af46.png

https://i-blog.csdnimg.cn/direct/92c8b570fe064c54a3c248751aa8bb32.png

输出例子

logger.info("接收到请求==>leeYqsbBasicVo = {}", leeYqsbBasicVo.toString());
logger.info("接收到请求==>leeYqsbBasicVo = {}", "哈哈");
2025-03-06 17:36:26 [http-nio-80-exec-2] INFO  LeeSxsDjController:50 - 接收到请求==>leeYqsbBasicVo = LeeYqsbBasicVo{lccb_sfdxyq='是', lccb_szsxsid='', key=''}
2025-03-06 17:40:02 [http-nio-80-exec-2] INFO  LeeSxsDjController:50 - 接收到请求==>leeYqsbBasicVo = 哈哈