优化基础公共日志记录

This commit is contained in:
白茶清欢 2025-06-08 12:05:35 +08:00
parent d936386d88
commit c4e421050e
3 changed files with 6 additions and 3 deletions

View File

@ -37,4 +37,6 @@ public class RecordField {
public static final String LOGGER_TYPE = "log_type"; // 日志类型
public static final String TIMESTAMP = "timestamp"; // 时间戳
public static final String TIME_FORMAT = "time_format"; // 格式化时间
}

View File

@ -31,7 +31,8 @@ public abstract class BaseProvider implements JsonProvider<ILoggingEvent> {
}
// 写入公共字段
protected void writeCommonField(RuntimeContext runtimeContext, JsonGenerator gen, String logType, String logData) throws IOException {
protected void writeCommonField(RuntimeContext runtimeContext, JsonGenerator gen, String logType, Object logData) throws IOException {
gen.writeNumberField(RecordField.TIMESTAMP, System.currentTimeMillis()); // 记录日志的时间
gen.writeStringField(RecordField.REQUEST_ID, runtimeContext.getRequestId());
gen.writeStringField(RecordField.TRACE_ID, runtimeContext.getTraceId());
gen.writeStringField(RecordField.SERVER_IP, runtimeContext.getServerIp());
@ -42,7 +43,7 @@ public abstract class BaseProvider implements JsonProvider<ILoggingEvent> {
gen.writeStringField(RecordField.REQUEST_CLIENT_IP, runtimeContext.getRequestInfo().getClientIp());
gen.writeNumberField(RecordField.COST, System.currentTimeMillis() - runtimeContext.getStartTimeStamp()); // 代表的是从请求开始, 到打印日志这一刻, 花费了多长时间
gen.writeStringField(RecordField.REQUEST_CONTENT_TYPE, runtimeContext.getRequestInfo().getRequestContentType() == null ? "" : runtimeContext.getRequestInfo().getRequestContentType());
gen.writeStringField(RecordField.CONTEXT_DATA,logData); // 除了公共字段值外的其他数据, 统一放在logData中
gen.writeObjectField(RecordField.CONTEXT_DATA,logData); // 除了公共字段值外的其他数据, 统一放在logData中
}
@Override

View File

@ -17,6 +17,6 @@ public class RequestInfoLogProvider extends BaseProvider {
return;
}
// 请求输入日志
writeCommonField(runtimeContext, gen, LogTypeEnum.REQUEST_INPUT.getLogType(), runtimeContext.getRequestInfo().toString());
writeCommonField(runtimeContext, gen, LogTypeEnum.REQUEST_INPUT.getLogType(), runtimeContext.getRequestInfo());
}
}