优化日志写入 + 增加日志实例获取

This commit is contained in:
白茶清欢 2025-06-07 22:26:35 +08:00
parent 3b4027f87b
commit 32d467da85
5 changed files with 37 additions and 5 deletions

View File

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

View File

@ -6,6 +6,11 @@ import lombok.Getter;
public enum LogTypeEnum { public enum LogTypeEnum {
REQUEST_INPUT("input", "请求输入信息"), REQUEST_INPUT("input", "请求输入信息"),
REQUEST_OUTPUT("output", "请求响应信息"), REQUEST_OUTPUT("output", "请求响应信息"),
DATABASE_RECORD("database", "数据库日志记录"),
CACHE_RECORD("cache", "缓存日志记录"),
API_RECORD("api", "第三方接口请求"),
MESSAGE_RECORD("message", "消息队列日志记录"),
TASK_RECORD("task", "常驻后台任务日志"),
; ;
private final String logType; private final String logType;
private final String description; private final String description;

View File

@ -1,4 +1,32 @@
package cn.zhangdeman.logger; package cn.zhangdeman.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// 各种日志实例获取, 配置是需要按照相关枚举值配置
public class LoggerInstance { public class LoggerInstance {
/**
* 请求输入日志记录实例
*/
private static final Logger REQUEST_INPUT_LOGGER = LoggerFactory.getLogger(LogTypeEnum.REQUEST_INPUT.getLogType());
/**
* 请求响应日志实例
*/
private static final Logger REQUEST_OUTPUT_LOGGER = LoggerFactory.getLogger(LogTypeEnum.REQUEST_OUTPUT.getLogType());
/**
* 数据库sql记录
*/
private static final Logger DATABASE_RECORD_LOGGER = LoggerFactory.getLogger(LogTypeEnum.DATABASE_RECORD.getLogType());
/**
* 缓存记录
*/
private static final Logger CACHE_RECORD_LOGGER = LoggerFactory.getLogger(LogTypeEnum.CACHE_RECORD.getLogType());
/**
* 消息队列记录
*/
private static final Logger MESSAGE_RECORD_LOGGER = LoggerFactory.getLogger(LogTypeEnum.MESSAGE_RECORD.getLogType());
/**
* 常驻后台任务日志记录
*/
private static final Logger TASK_RECORD_LOGGER = LoggerFactory.getLogger(LogTypeEnum.TASK_RECORD.getLogType());
} }

View File

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

View File

@ -17,7 +17,6 @@ public class ResponseInfoLogProvider extends BaseProvider {
return; return;
} }
// 请求输入日志 // 请求输入日志
writeCommonField(runtimeContext, gen, LogTypeEnum.REQUEST_OUTPUT.getLogType()); writeCommonField(runtimeContext, gen, LogTypeEnum.REQUEST_OUTPUT.getLogType(), runtimeContext.getResponse().toString());
gen.writeStringField(RecordField.RESPONSE_INFO, runtimeContext.getResponse().toString());
} }
} }