This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
@@ -35,6 +36,19 @@ func Middleware() fiber.Handler {
|
||||
startTime := time.Now()
|
||||
c.Locals(constants.ContextKeyStartTime, startTime)
|
||||
|
||||
// 注入请求上下文,供 Service 层审计日志复用
|
||||
ctx := c.UserContext()
|
||||
if rid := c.Locals(constants.ContextKeyRequestID); rid != nil {
|
||||
if requestID, ok := rid.(string); ok && requestID != "" {
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyRequestID, requestID)
|
||||
}
|
||||
}
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyIP, c.IP())
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserAgent, c.Get("User-Agent"))
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyRequestPath, c.Path())
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyRequestMethod, c.Method())
|
||||
c.SetUserContext(ctx)
|
||||
|
||||
// 获取请求 body(在 c.Next() 之前读取)
|
||||
requestBody := truncateBody(c.Body(), MaxBodyLogSize)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user