Spring Boot 2.7.18项目中Log4j无法记录API请求响应JSON的问题求助
Spring Boot 2.7.18项目中Log4j无法记录API请求响应JSON的问题求助
大家好,我目前在维护一个Spring Boot项目,技术栈情况如下:
- Spring Boot 2.7.18
- Log4j2
- Tomcat 9
- JDK 8
- 数据库用的是公司暂不升级的SQL Server 2008 R2
应用仅部署在公司内网,不对外网开放。
现在我碰到了个棘手的问题:我想通过Log4j2来记录API接口的请求和响应JSON数据,但怎么都没效果,完全看不到对应的日志输出。
下面是我的项目配置文件,麻烦各位帮忙看看哪里出问题了:
pom.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>ycrmyy</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>ycrmyy</name> <description>ycrmyy</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>1.8</java.version> <mssql.jdbc.version>10.2.1.jre8</mssql.jdbc.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.parameters>true</maven.compiler.parameters> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> <!-- key point --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>${mssql.jdbc.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.32</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.DemoApplication</mainClass> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>
log4j2-spring.xml 配置(resources目录下)
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" shutdownHook="disable"> <Properties> <Property name="logPath">D:/logs/</Property> <Property name="jsonTpl">classpath:LogstashJsonEventLayoutV1.json</Property> </Properties> <Appenders> <Console name="CONSOLE"> <JsonTemplateLayout eventTemplateUri="${jsonTpl}"/> </Console> </Appenders> <!-- 注:原配置内容到此处结束,未完整给出后续配置 --> </Configuration>
我已经确认Log4j2的依赖是正确引入的,也排除了Spring Boot默认的logging starter,但就是看不到请求响应的JSON日志。有没有大佬能帮我分析下问题出在哪?比如是不是日志布局配置有缺失,或者还需要额外的拦截器/过滤器来捕获请求响应内容?
内容来源于stack exchange
相关产品推荐
相关产品推荐

