提交 3350aa35 authored 作者: mooncake's avatar mooncake

logger support startspan

上级 52857d5c
package logger
import (
"context"
"strings"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sf"
"go.uber.org/zap"
)
......@@ -75,3 +77,33 @@ func Sync() error {
func WithFields(fields ...Field) *zap.Logger {
return GetWithSkip(0).With(fields...)
}
func StartSpan(ctx context.Context) *zap.SugaredLogger {
return getSugaredLogger().With(TraceIDField(ctx))
}
func TraceIDField(c context.Context) zap.Field {
return zap.String(ctxTraceIDKey, CtxRequestID(c))
}
func CtxRequestID(c context.Context) string {
traceID := ""
v := c.Value(ctxTraceIDKey)
if str, ok := v.(string); ok {
if str != "" {
traceID = str
}
}
if traceID == "" {
traceID = sf.GenerateID().String()
c = context.WithValue(c, ctxTraceIDKey, traceID)
}
return traceID
}
const (
ctxTraceIDKey = "X-Request-ID"
)
......@@ -5,8 +5,6 @@ import (
"strconv"
"github.com/bwmarrin/snowflake"
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xerror"
)
......@@ -84,7 +82,6 @@ func Init(n int) error {
return e
}
node = nod
logger.Info("[xsf] node " + cast.ToString(n))
return nil
}
......@@ -101,9 +98,16 @@ func (x ID) Int() int {
}
func GenerateID() ID {
CheckNil()
return ID(node.Generate())
}
func CheckNil() {
if node == nil {
node, _ = snowflake.NewNode(9)
}
}
func ParseInt64(val int64) ID {
return ID(snowflake.ParseInt64(val))
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论