Documentation
¶
Index ¶
- Constants
- func BindCtx(ctx context.Context) (release func())
- func CatchPanic(panicCallback func(err interface{}))
- func ClearReqId()
- func Debug(args ...interface{})
- func Debugf(template string, args ...interface{})
- func DebugfCtx(ctx context.Context, template string, args ...interface{})
- func Debugw(msg string, kv ...any)
- func DebugwCtx(ctx context.Context, msg string, kv ...any)
- func Error(args ...interface{})
- func Errorf(template string, args ...interface{})
- func ErrorfCtx(ctx context.Context, template string, args ...interface{})
- func Errorw(msg string, kv ...any)
- func ErrorwCtx(ctx context.Context, msg string, kv ...any)
- func Fatal(args ...interface{})
- func Fatalf(template string, args ...interface{})
- func FormatError(err error) string
- func GRPCError(err error, code codes.Code, operation, message string) error
- func GetReqId() string
- func Go(fn func())
- func GoSafe(fn func(), onPanic func(err interface{}))
- func Info(args ...interface{})
- func Infof(template string, args ...interface{})
- func InfofCtx(ctx context.Context, template string, args ...interface{})
- func Infow(msg string, kv ...any)
- func InfowCtx(ctx context.Context, msg string, kv ...any)
- func Must(err error)
- func NewError(operation, format string, args ...any) error
- func PrintStack(skip int)
- func ResetChildCounter(reqId string)
- func SetErrorSourceRoot(root string)
- func SetLogLevel(l Level)
- func SetOutput(w io.Writer)
- func SetReqId(reqId string)
- func SetReqIdFromContext(ctx context.Context)
- func Warn(args ...interface{})
- func Warnf(template string, args ...interface{})
- func WarnfCtx(ctx context.Context, template string, args ...interface{})
- func Warnw(msg string, kv ...any)
- func WarnwCtx(ctx context.Context, msg string, kv ...any)
- func WithReqID(ctx context.Context, reqId string) context.Context
- func WrapError(err error, operation string) error
- type Field
- type GormLogger
- func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{})
- func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{})
- func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface
- func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l *GormLogger) Warn(ctx context.Context, msg string, data ...interface{})
- type Level
- type TracedError
Constants ¶
const (
ReqIDKey contextKey = "req_id"
)
Variables ¶
This section is empty.
Functions ¶
func BindCtx ¶
BindCtx 把 ctx 中的 req_id 绑定到当前 goroutine。 推荐在每个对外入口(如 CacheDB.Transaction、CallBeforeQuery)调用一次:
release := log.BindCtx(ctx) defer release()
即使 ctx 为 nil 或没有 req_id,也安全(返回 no-op release)。
func CatchPanic ¶
func CatchPanic(panicCallback func(err interface{}))
CatchPanic 在当前 goroutine 内 recover 并打印完整栈。
用法:
go func() {
defer log.CatchPanic(nil)
// ... 业务代码
}()
panicCallback 可选;若不为 nil,会在日志打印之后被调用, 通常用于上报告警 / 释放资源。
注:本函数使用 log.Errorf / log.Error,因此 panic 日志会自动带上当前 goroutine 的 reqId。
func ClearReqId ¶
func ClearReqId()
func FormatError ¶
FormatError returns the error message followed by every captured source frame.
func Go ¶
func Go(fn func())
Go 启动新 goroutine 并传递 reqId。
行为:
- 父 goroutine 没有 reqId → 子 goroutine 也没有
- 父 goroutine 有 reqId="req-abc" → 子 goroutine reqId="req-abc.1", 再启第二个子 → "req-abc.2",以此类推
- 子 goroutine 内再调用 Go() → 孙子 reqId="req-abc.1.1",形成树状层级
- 子 goroutine 退出时自动 ClearReqId,避免 goid 复用串号
- 子 goroutine 内若 panic:自动 recover,打印完整栈与 reqId,**不会** 拖垮进程
Example:
log.SetReqId("req-abc")
log.Go(func() {
log.Infow("child task") // [req-abc.1]
log.Go(func() {
log.Infow("grandchild task") // [req-abc.1.1]
})
})
func GoSafe ¶
func GoSafe(fn func(), onPanic func(err interface{}))
GoSafe 与 Go 行为一致,但允许传入 panic 回调(如告警 / metrics 上报)。
回调在日志打印 **之后** 执行,不会再次 panic 出去。
func PrintStack ¶
func PrintStack(skip int)
func ResetChildCounter ¶
func ResetChildCounter(reqId string)
ResetChildCounter 清理某个 parent reqId 的子计数器。可在请求结束时调用,避免长期累积。 不调用也不会影响功能(仅占少量内存)。
func SetErrorSourceRoot ¶
func SetErrorSourceRoot(root string)
SetErrorSourceRoot controls how source paths are shortened by FormatError. An empty root uses the process working directory.
func SetLogLevel ¶
func SetLogLevel(l Level)
func SetReqIdFromContext ¶
Types ¶
type GormLogger ¶
type GormLogger struct {
LogLevel logger.LogLevel
SlowThreshold time.Duration
IgnoreRecordNotFoundError bool
logger.Interface
}
GormLogger 封装gorm.logger以适应公共库的log风格
func (*GormLogger) Error ¶
func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{})
Error print error messages
func (*GormLogger) Info ¶
func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{})
Info print info
func (*GormLogger) LogMode ¶
func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface
LogMode log mode
type TracedError ¶
TracedError is one contextual source frame in an error chain.
func (*TracedError) Error ¶
func (e *TracedError) Error() string
func (*TracedError) Unwrap ¶
func (e *TracedError) Unwrap() error