Files
workspace/code/allapp/allapp-go/logx/field.go
T
2026-01-13 22:18:01 +08:00

60 lines
1.1 KiB
Go

package logx
import (
"context"
"github.com/cloudwego/hertz/pkg/common/hlog"
"go.uber.org/zap"
)
// getRequestID 从 context 中取 RequestID,如果没有则返回空字符串
func getRequestID(ctx context.Context) string {
if ctx == nil {
return ""
}
if rid, ok := ctx.Value("RequestID").(string); ok {
return rid
}
return ""
}
func CtxInfo(ctx context.Context, v ...interface{}) {
rid := getRequestID(ctx)
if rid != "" {
v = append(v, zap.String("RequestID", rid))
}
hlog.Info(v...)
}
func CtxError(ctx context.Context, v ...interface{}) {
rid := getRequestID(ctx)
if rid != "" {
v = append(v, zap.String("RequestID", rid))
}
hlog.Error(v...)
}
func CtxWarn(ctx context.Context, v ...interface{}) {
rid := getRequestID(ctx)
if rid != "" {
v = append(v, zap.String("RequestID", rid))
}
hlog.Warn(v...)
}
func CtxDebug(ctx context.Context, v ...interface{}) {
rid := getRequestID(ctx)
if rid != "" {
v = append(v, zap.String("RequestID", rid))
}
hlog.Debug(v...)
}
func CtxFatal(ctx context.Context, v ...interface{}) {
rid := getRequestID(ctx)
if rid != "" {
v = append(v, zap.String("RequestID", rid))
}
hlog.Fatal(v...)
}