fix ivyz7c9d
fix external_logger
This commit is contained in:
@@ -152,7 +152,7 @@ func createFileWriter(logDir, level string, config ExternalServiceLevelFileConfi
|
||||
// 按天分隔:logs/westdex/2024-01-01/westdex_info.log
|
||||
date := time.Now().Format("2006-01-02")
|
||||
dateDir := filepath.Join(logDir, date)
|
||||
|
||||
|
||||
// 确保日期目录存在
|
||||
if err := os.MkdirAll(dateDir, 0755); err != nil {
|
||||
// 如果创建日期目录失败,回退到根目录
|
||||
@@ -175,10 +175,11 @@ func createFileWriter(logDir, level string, config ExternalServiceLevelFileConfi
|
||||
}
|
||||
|
||||
// LogRequest 记录请求日志
|
||||
func (e *ExternalServiceLogger) LogRequest(requestID, apiCode string, url interface{}, params interface{}) {
|
||||
func (e *ExternalServiceLogger) LogRequest(requestID, transactionID, apiCode string, url interface{}, params interface{}) {
|
||||
e.logger.Info(fmt.Sprintf("%s API请求", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Any("url", url),
|
||||
zap.Any("params", params),
|
||||
@@ -187,10 +188,11 @@ func (e *ExternalServiceLogger) LogRequest(requestID, apiCode string, url interf
|
||||
}
|
||||
|
||||
// LogResponse 记录响应日志
|
||||
func (e *ExternalServiceLogger) LogResponse(requestID, apiCode string, statusCode int, response []byte, duration time.Duration) {
|
||||
func (e *ExternalServiceLogger) LogResponse(requestID, transactionID, apiCode string, statusCode int, response []byte, duration time.Duration) {
|
||||
e.logger.Info(fmt.Sprintf("%s API响应", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Int("status_code", statusCode),
|
||||
zap.String("response", string(response)),
|
||||
@@ -199,11 +201,27 @@ func (e *ExternalServiceLogger) LogResponse(requestID, apiCode string, statusCod
|
||||
)
|
||||
}
|
||||
|
||||
// LogResponseWithID 记录包含响应ID的响应日志
|
||||
func (e *ExternalServiceLogger) LogResponseWithID(requestID, transactionID, apiCode string, statusCode int, response []byte, duration time.Duration, responseID string) {
|
||||
e.logger.Info(fmt.Sprintf("%s API响应", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Int("status_code", statusCode),
|
||||
zap.String("response", string(response)),
|
||||
zap.Duration("duration", duration),
|
||||
zap.String("response_id", responseID),
|
||||
zap.String("timestamp", time.Now().Format(time.RFC3339)),
|
||||
)
|
||||
}
|
||||
|
||||
// LogError 记录错误日志
|
||||
func (e *ExternalServiceLogger) LogError(requestID, apiCode string, err error, params interface{}) {
|
||||
func (e *ExternalServiceLogger) LogError(requestID, transactionID, apiCode string, err error, params interface{}) {
|
||||
e.logger.Error(fmt.Sprintf("%s API错误", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Error(err),
|
||||
zap.Any("params", params),
|
||||
@@ -211,6 +229,20 @@ func (e *ExternalServiceLogger) LogError(requestID, apiCode string, err error, p
|
||||
)
|
||||
}
|
||||
|
||||
// LogErrorWithResponseID 记录包含响应ID的错误日志
|
||||
func (e *ExternalServiceLogger) LogErrorWithResponseID(requestID, transactionID, apiCode string, err error, params interface{}, responseID string) {
|
||||
e.logger.Error(fmt.Sprintf("%s API错误", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Error(err),
|
||||
zap.Any("params", params),
|
||||
zap.String("response_id", responseID),
|
||||
zap.String("timestamp", time.Now().Format(time.RFC3339)),
|
||||
)
|
||||
}
|
||||
|
||||
// LogInfo 记录信息日志
|
||||
func (e *ExternalServiceLogger) LogInfo(message string, fields ...zap.Field) {
|
||||
allFields := []zap.Field{zap.String("service", e.serviceName)}
|
||||
@@ -244,7 +276,7 @@ func (e *ExternalServiceLogger) CleanupOldDateDirs() error {
|
||||
}
|
||||
|
||||
logDir := filepath.Join(e.config.LogDir, e.serviceName)
|
||||
|
||||
|
||||
// 读取日志目录
|
||||
entries, err := os.ReadDir(logDir)
|
||||
if err != nil {
|
||||
@@ -256,7 +288,7 @@ func (e *ExternalServiceLogger) CleanupOldDateDirs() error {
|
||||
if errorConfig, exists := e.config.LevelConfigs["error"]; exists && errorConfig.MaxAge > 0 {
|
||||
maxAge = errorConfig.MaxAge
|
||||
}
|
||||
|
||||
|
||||
cutoffTime := time.Now().AddDate(0, 0, -maxAge)
|
||||
|
||||
for _, entry := range entries {
|
||||
|
||||
Reference in New Issue
Block a user