Documentation
¶
Index ¶
- func Debug(msg string, args ...any)
- func DebugCtx(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorCtx(ctx context.Context, msg string, args ...any)
- func GetLevel() slog.Level
- func Info(msg string, args ...any)
- func InfoCtx(ctx context.Context, msg string, args ...any)
- func LevelHandler() http.Handler
- func NewTraceHandler(h slog.Handler, opts ...TraceHandlerOption) slog.Handler
- func SetLevel(level slog.Level)
- func SetLevelByName(name string) error
- func Sync() error
- func Warn(msg string, args ...any)
- func WarnCtx(ctx context.Context, msg string, args ...any)
- type TraceHandlerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LevelHandler ¶
LevelHandler returns an http.Handler that exposes dynamic log-level control.
GET /loglevel -> {"level":"info"}
PUT /loglevel -> body {"level":"debug"}, returns 200 {"level":"debug"} or 400 on error
func NewTraceHandler ¶
func NewTraceHandler(h slog.Handler, opts ...TraceHandlerOption) slog.Handler
TraceHandler 包装一个 slog.Handler,在每条日志记录上自动追加当前 OpenTelemetry span 的 trace_id / span_id(取自记录的 context)。 仅当 context 中存在有效 span 时才追加,因此需用 *Context 系列方法记录日志:
base := slog.NewJSONHandler(os.Stdout, opts) slog.SetDefault(slog.New(logger.NewTraceHandler(base))) slog.InfoContext(ctx, "handled request") // 自动带上 trace_id/span_id
可通过 WithTraceKey / WithSpanKey 自定义字段名:
logger.NewTraceHandler(base, logger.WithTraceKey("traceID"), logger.WithSpanKey("spanID"))
func SetLevel ¶
SetLevel dynamically sets the global log level; takes effect immediately without restart.
func SetLevelByName ¶
SetLevelByName sets the log level by name: "debug"/"info"/"warn"/"error" (case-insensitive). Returns an error for unrecognized names without changing the current level.
Types ¶
type TraceHandlerOption ¶ added in v0.7.7
type TraceHandlerOption func(*traceHandler)
TraceHandlerOption 配置 TraceHandler 的可选参数。
func WithSpanKey ¶ added in v0.7.7
func WithSpanKey(key string) TraceHandlerOption
WithSpanKey 自定义 span ID 字段名,默认 "span_id"。
func WithTraceKey ¶ added in v0.7.7
func WithTraceKey(key string) TraceHandlerOption
WithTraceKey 自定义 trace ID 字段名,默认 "trace_id"。