REQ-3201: 记录请求

This commit is contained in:
yanglin 2024-12-27 10:58:56 +08:00
parent db59ec18e4
commit 3a602139b6
3 changed files with 19 additions and 3 deletions

View File

@ -5,6 +5,9 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.HashMap;
import java.util.Map;
/**
* @author yanglin
*/
@ -14,9 +17,19 @@ public class CardRequestContext<T> {
private final String batchNo = UUIDUtil.uuidString();
private final T request;
private final Map<String, Object> logContents = new HashMap<>();
public static <T> CardRequestContext<T> create(T request) {
return new CardRequestContext<>(request);
CardRequestContext<T> context = new CardRequestContext<>(request);
context.addLogContent("request", request);
return context;
}
public CardRequestContext<T> addLogContent(String key, Object value) {
if (value == null ||logContents.containsKey(key))
return this;
logContents.put(key, value);
return this;
}
}

View File

@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author yanglin
@ -72,6 +73,8 @@ public class CardLoggers {
log.setError(exception == null ? "" : Throwables.getStackTraceAsString(exception));
log.addLogContent("request", requestContext.getRequest());
log.addLogContent("isCardDelete", card.getIsDelete());
for (Map.Entry<String, Object> entry : requestContext.getLogContents().entrySet())
log.addLogContent(entry.getKey(), entry.getValue());
}
cardLogDao.saveBatch(logs);
}

View File

@ -124,10 +124,10 @@ public class CardLog extends BaseEntityExt<CardLog> {
@TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
private Card.RecordExt recordExt;
public void addLogContent(String name, Object value) {
public void addLogContent(String key, Object value) {
if (logContent == null)
logContent = new JSONObject();
logContent.put(name, value);
logContent.put(key, value);
}
@Setter