如何优雅的在 SpringBoot 中打印 Request&Response 日志

springmarker:本来想写个 springboot-starter 来做 Request&Response 日志打印的,但是发现逛了一圈谷歌,发现基本都是用拦截器做的。

使用拦截器做本身没什么问题,但是 HttpServletRequest 读取过一次后,body 就不能再读取了,解决办法就是在 Filter 中自己提前包装一个可重复读的 Request,但是觉得这样做有点麻烦而且不那么优雅。

请教一下有什么优雅的办法在 SpringBoot 中打印 Request&Response 日志吗?

MoHen9:可以用 AOP 打印,参考一下我的实现,https://github.com/benefitj/springboot-doughnut/blob/master/aop/src/main/java/com/benefitj/spring/aop/WebRequestAspect.java

Kyle18Tang:https://github.com/zalando/logbook,我们已经用了,可以各种自定义,非常适合。

springmarker:@MoHen9 #1 AOP 早就试过了,但是 ctrl 层传入传出都是对象,打印日志还得序列化一下,感觉有点浪费

@Kyle18Tang #2 这个在 stackoverflow 见过,还没试过,想自己写个轻量级的

frankly123:/**
* @author wangyiwen
* @version 1.0 Created in 2020/7/14 18:36
* 该注入是为了可以获取到包装过的 httpRequest
*/
public class WrapperRequestFilter extends AbstractRequestLoggingFilter {

/**
* Concrete subclasses should implement this method to write a log message
* <i>before</i> the request is processed.
*
* @param request current HTTP request
* @param message the message to log
*/
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
//do nothing
}

/**
* Concrete subclasses should implement this method to write a log message
* <i>after</i> the request is processed.
*
* @param request current HTTP request
* @param message the message to log
*/
@Override
protected void afterRequest(HttpServletRequest request, String message) {
//do nothing
}
}
---------------------------------------------------------------------------------------------------
@Bean
public WrapperRequestFilter wrapperRequestFilter() {
WrapperRequestFilter wrapperRequestFilter = new WrapperRequestFilter();
wrapperRequestFilter.setIncludeQueryString(true);
wrapperRequestFilter.setIncludeClientInfo(true);
wrapperRequestFilter.setIncludeHeaders(true);
wrapperRequestFilter.setIncludePayload(true);
wrapperRequestFilter.setMaxPayloadLength(99999);
return wrapperRequestFilter;
}

SpringBoot 配置属性的中文说明(https://prop.springboot.io/)

KevinBlandy:SpringBoot 所有配置项的中文说明(使用Deepl翻译),由 SpringBoot 中文社区 整理提供。(还有部分没翻译完) 篇幅有限,配置项实在太多。查看所有的配置可以戳 https://prop.springboot.io/

PHP中的“and”和“&&”运算符之间有什么区别吗? - php

我有以下代码,不认为并且不是必需的,即应该使用&&,因为没有什么可以分配左边的部分?if($_REQUEST['foo'] != 'abc' and $_REQUEST['bar'] == 'def') { echo "all your base…

赚点辛苦费,欢迎各位老板&大佬咨询购买惠普产品

Caan07:[赚点辛苦费] 主营惠普商用 PC/商务笔记本,保修一切按照惠普官方来执行。 贴图并不是所有型号和配置,有些产品特定工厂生产,或者有些型号不支持 CTO 特配,麻烦微信确认型号再确定是否支持特配。 有 INTEL 平台的,也有 AMD 平台的,也有国产芯片机型(不支持 CTO ) 品牌机,价格肯定不被各位 DIY 大佬看上,希望各位 DIY 大…

springboot 敏捷后台开发框架

colinnj:系统简介 JavaWeb 专业版,企业级开发框架 SpringBoot+Layui+Thymeleaf+MybatisPlus 开发权限(RBAC)及内容管理框架,框架中集成了权限管理、模块管理,数据库管理、富文本编辑器(已集成 ueditor,kindeditor),后台支持多主题切换、布局管理、广告管理、配置管理、字典管理、切图管理、CM…

springboot 权限管理系统

colinnj:系统简介 JavaWeb 专业版,企业级开发框架 SpringBoot+Layui+Thymeleaf+MybatisPlus 开发权限(RBAC)及内容管理框架,框架中集成了权限管理、模块管理,数据库管理、富文本编辑器(已集成 ueditor,kindeditor),后台支持多主题切换、布局管理、广告管理、配置管理、字典管理、切图管理、CM…