Skip to content

Commit

Permalink
🎨 将 request payload 改为 request body, response 同上,方便用户理解
Browse files Browse the repository at this point in the history
link gh-237
  • Loading branch information
Hccake committed Jun 27, 2023
1 parent cb64179 commit fc7923f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public FilterRegistrationBean<AbstractAccessLogFilter> accessLogFilterRegistrati
AbstractAccessLogFilter accessLogFilter = logFilterObjectProvider.getIfAvailable();
if (accessLogFilter == null) {
accessLogFilter = new DefaultAccessLogFilter(accessLogProperties.getSettings());
accessLogFilter.setMaxPayloadLength(accessLogProperties.getMaxPayloadLength());
accessLogFilter.setMaxBodyLength(accessLogProperties.getMaxBodyLength());
}
FilterRegistrationBean<AbstractAccessLogFilter> registrationBean = new FilterRegistrationBean<>(
accessLogFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.ballcat.web.accesslog.AccessLogRule;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -48,21 +48,15 @@ public class AccessLogProperties {
private Integer filterOrder = -1000;

/**
* 最大的 payload 长度
* 记录的最大的 body 长度
*/
private Integer maxPayloadLength = AbstractAccessLogFilter.DEFAULT_MAX_PAYLOAD_LENGTH;
private Integer maxBodyLength = AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH;

/**
* 访问日志的设置集合
* 访问日志记录的规则列表
* <p>
* 以当前 request uri 匹配中的第一个规则为准,所以通用性的规则(例如 /**)应放在最后一项
*/
private List<AccessLogRule> settings = Collections.singletonList(new AccessLogRule().setUrlPattern("/**"));

//
// /**
// * 忽略的Url匹配规则,Ant风格
// */
// private List<String> ignoreUrlPatterns = Arrays.asList("/actuator/**",
// "/webjars/**", "/favicon.ico",
// "/swagger-ui/**", "/bycdao-ui/**", "/captcha/get");

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*/
public abstract class AbstractAccessLogFilter extends OncePerRequestFilter {

public static final int DEFAULT_MAX_PAYLOAD_LENGTH = 256;
public static final int DEFAULT_MAX_BODY_LENGTH = 256;

private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
private int maxBodyLength = DEFAULT_MAX_BODY_LENGTH;

/**
* Same contract as for {@code doFilter}, but guaranteed to be just invoked once per
Expand Down Expand Up @@ -72,7 +72,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

HttpServletRequest requestToUse;
if (accessLogSettings.shouldRecordRequestPayload()) {
if (accessLogSettings.shouldRecordRequestBody()) {
// 包装 request,以保证可以重复读取body
// spring 提供的 ContentCachingRequestWrapper,在 body 没有被程序使用时,获取到的 body 缓存为空
// 且对于 form data,会混淆 payload 和 query string
Expand All @@ -83,7 +83,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

HttpServletResponse responseToUse;
if (accessLogSettings.shouldRecordResponsePayload()) {
if (accessLogSettings.shouldRecordResponseBody()) {
// 包装 response,便于重复获取 body
responseToUse = new ContentCachingResponseWrapper(response);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

@Nullable
protected String getRequestPayload(HttpServletRequest request) {
protected String getRequestBody(HttpServletRequest request) {
RepeatBodyRequestWrapper wrapper = WebUtils.getNativeRequest(request, RepeatBodyRequestWrapper.class);
if (wrapper == null) {
return null;
Expand All @@ -153,7 +153,7 @@ protected String getRequestPayload(HttpServletRequest request) {
}

@Nullable
protected String getResponsePayload(HttpServletResponse response) {
protected String getResponseBody(HttpServletResponse response) {
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response,
ContentCachingResponseWrapper.class);
if (wrapper == null) {
Expand All @@ -165,7 +165,7 @@ protected String getResponsePayload(HttpServletResponse response) {
@Nullable
protected String getMessagePayload(byte[] buf, String characterEncoding) {
if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength());
int length = Math.min(buf.length, getMaxBodyLength());
try {
return new String(buf, 0, length, characterEncoding);
}
Expand All @@ -176,13 +176,13 @@ protected String getMessagePayload(byte[] buf, String characterEncoding) {
return null;
}

public void setMaxPayloadLength(int maxPayloadLength) {
Assert.isTrue(maxPayloadLength >= 0, "'maxPayloadLength' must be greater than or equal to 0");
this.maxPayloadLength = maxPayloadLength;
public void setMaxBodyLength(int maxBodyLength) {
Assert.isTrue(maxBodyLength >= 0, "'maxBodyLength' must be greater than or equal to 0");
this.maxBodyLength = maxBodyLength;
}

protected int getMaxPayloadLength() {
return this.maxPayloadLength;
protected int getMaxBodyLength() {
return this.maxBodyLength;
}

protected boolean shouldLog(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Set;

/**
* 访问日志规则
*
Expand Down Expand Up @@ -47,11 +45,11 @@ public class AccessLogRule {
/**
* 记录请求体
*/
private boolean includeRequestPayload = false;
private boolean includeRequestBody = false;

/**
* 记录响应体
*/
private boolean includeResponsePayload = false;
private boolean includeResponseBody = false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public class AccessLogSettings {
/**
* 记录请求体
*/
private final boolean includeRequestPayload;
private final boolean includeRequestBody;

/**
* 记录响应体
*/
private final boolean includeResponsePayload;
private final boolean includeResponseBody;

public boolean enabled() {
return this.enabled;
Expand All @@ -55,12 +55,12 @@ public boolean shouldRecordQueryString() {
return this.includeQueryString;
}

public boolean shouldRecordRequestPayload() {
return this.includeRequestPayload;
public boolean shouldRecordRequestBody() {
return this.includeRequestBody;
}

public boolean shouldRecordResponsePayload() {
return this.includeResponsePayload;
public boolean shouldRecordResponseBody() {
return this.includeResponseBody;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ protected AccessLogSettings getAccessLogSettings(HttpServletRequest request) {
return AccessLogSettings.builder()
.enabled(!logRule.isIgnore())
.includeQueryString(logRule.isIncludeQueryString())
.includeRequestPayload(logRule.isIncludeRequestPayload())
.includeResponsePayload(logRule.isIncludeResponsePayload())
.includeRequestBody(logRule.isIncludeRequestBody())
.includeResponseBody(logRule.isIncludeResponseBody())
.build();
}
}
Expand Down Expand Up @@ -111,17 +111,17 @@ protected void afterRequest(HttpServletRequest request, HttpServletResponse resp
String ipAddr = IpUtils.getIpAddr(request);
msg.append(", client=").append(ipAddr);

if (accessLogSettings.shouldRecordRequestPayload()) {
String payload = getRequestPayload(request);
if (accessLogSettings.shouldRecordRequestBody()) {
String payload = getRequestBody(request);
if (payload != null) {
msg.append(", request payload=").append(payload);
msg.append(", request body=").append(payload);
}
}

if (accessLogSettings.shouldRecordResponsePayload()) {
String payload = getResponsePayload(response);
if (accessLogSettings.shouldRecordResponseBody()) {
String payload = getResponseBody(response);
if (payload != null) {
msg.append(", response payload=").append(payload);
msg.append(", response body=").append(payload);
}
}

Expand Down

0 comments on commit fc7923f

Please sign in to comment.