mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
refactor(log/httptrace-pro): 优化日志记录
1.新增是否打印配置,开启后可打印访问日志(类似于 Nginx access log) 2.TIME_TAKEN 请求耗时,调整为必然记录 3.REQUEST_PARAM 请求参数、RESPONSE_PARAM 响应参数调整为默认记录 4.包含信息配置:BODY 和 PARAM 调整为互斥,包含 BODY 则对应 PARAM 不记录 5.请求 URI 不再记录协议、域名等信息 6.修复部分请求、响应信息记录错误
This commit is contained in:
@@ -38,6 +38,11 @@ public class LogProperties {
|
||||
*/
|
||||
private boolean enabled = false;
|
||||
|
||||
/**
|
||||
* 是否打印日志,开启后可打印访问日志(类似于 Nginx access log)
|
||||
*/
|
||||
private Boolean isPrint = false;
|
||||
|
||||
/**
|
||||
* 包含信息
|
||||
*/
|
||||
|
@@ -31,6 +31,8 @@ import top.charles7c.continew.starter.log.common.annotation.Log;
|
||||
import top.charles7c.continew.starter.log.common.dao.LogDao;
|
||||
import top.charles7c.continew.starter.log.common.enums.Include;
|
||||
import top.charles7c.continew.starter.log.common.model.LogRecord;
|
||||
import top.charles7c.continew.starter.log.common.model.LogRequest;
|
||||
import top.charles7c.continew.starter.log.common.model.LogResponse;
|
||||
import top.charles7c.continew.starter.log.httptracepro.autoconfigure.LogProperties;
|
||||
|
||||
import java.time.Clock;
|
||||
@@ -48,13 +50,19 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final LogDao dao;
|
||||
private final LogProperties properties;
|
||||
private final TransmittableThreadLocal<Clock> timestampTtl = new TransmittableThreadLocal<>();
|
||||
private final TransmittableThreadLocal<LogRecord.Started> timestampTtl = new TransmittableThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
|
||||
@NonNull Object handler) {
|
||||
Clock timestamp = Clock.systemUTC();
|
||||
if (this.isRequestRecord(handler)) {
|
||||
timestampTtl.set(Clock.systemUTC());
|
||||
RecordableServletHttpRequest sourceRequest = new RecordableServletHttpRequest(request);
|
||||
if (Boolean.TRUE.equals(properties.getIsPrint())) {
|
||||
log.info("[{}] {}", sourceRequest.getMethod(), sourceRequest.getUri());
|
||||
}
|
||||
LogRecord.Started startedLogRecord = LogRecord.start(timestamp, sourceRequest);
|
||||
timestampTtl.set(startedLogRecord);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -62,26 +70,28 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
|
||||
@NonNull Object handler, Exception e) {
|
||||
Clock timestamp = timestampTtl.get();
|
||||
if (null == timestamp) {
|
||||
LogRecord.Started startedLogRecord = timestampTtl.get();
|
||||
if (null == startedLogRecord) {
|
||||
return;
|
||||
}
|
||||
timestampTtl.remove();
|
||||
Set<Include> includeSet = properties.getInclude();
|
||||
RecordableServletHttpRequest sourceRequest = new RecordableServletHttpRequest(request);
|
||||
try {
|
||||
LogRecord.Started startedLogRecord = LogRecord.start(timestamp, sourceRequest);
|
||||
RecordableServletHttpResponse sourceResponse = new RecordableServletHttpResponse(response, response.getStatus());
|
||||
LogRecord finishedLogRecord = startedLogRecord.finish(sourceResponse, includeSet);
|
||||
LogRecord finishedLogRecord = startedLogRecord.finish(new RecordableServletHttpResponse(response, response.getStatus()), includeSet);
|
||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||
// 记录日志描述
|
||||
if (includeSet.contains(Include.DESCRIPTION)) {
|
||||
// 记录日志描述
|
||||
this.logDescription(finishedLogRecord, handlerMethod);
|
||||
}
|
||||
// 记录所属模块
|
||||
if (includeSet.contains(Include.MODULE)) {
|
||||
// 记录所属模块
|
||||
this.logModule(finishedLogRecord, handlerMethod);
|
||||
}
|
||||
if (Boolean.TRUE.equals(properties.getIsPrint())) {
|
||||
LogRequest logRequest = finishedLogRecord.getRequest();
|
||||
LogResponse logResponse = finishedLogRecord.getResponse();
|
||||
log.info("[{}] {} {} {}ms", logRequest.getMethod(), logRequest.getUri(), logResponse.getStatus(), finishedLogRecord.getTimeTaken().toMillis());
|
||||
}
|
||||
dao.add(finishedLogRecord);
|
||||
} catch (Exception ex) {
|
||||
log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex);
|
||||
|
@@ -54,15 +54,13 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest
|
||||
public URI getUri() {
|
||||
String queryString = request.getQueryString();
|
||||
if (StrUtil.isBlank(queryString)) {
|
||||
return URI.create(request.getRequestURL().toString());
|
||||
return URI.create(request.getRequestURI());
|
||||
}
|
||||
try {
|
||||
StringBuffer urlBuffer = this.appendQueryString(queryString);
|
||||
return new URI(urlBuffer.toString());
|
||||
return new URI(this.appendQueryString(queryString));
|
||||
} catch (URISyntaxException e) {
|
||||
String encoded = UriUtils.encodeQuery(queryString, StandardCharsets.UTF_8);
|
||||
StringBuffer urlBuffer = this.appendQueryString(encoded);
|
||||
return URI.create(urlBuffer.toString());
|
||||
return URI.create(this.appendQueryString(encoded));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +91,7 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest
|
||||
: Collections.unmodifiableMap(request.getParameterMap());
|
||||
}
|
||||
|
||||
private StringBuffer appendQueryString(String queryString) {
|
||||
return request.getRequestURL().append(StringConstants.QUESTION_MARK).append(queryString);
|
||||
private String appendQueryString(String queryString) {
|
||||
return request.getRequestURI() + StringConstants.QUESTION_MARK + queryString;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user