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) 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)
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 ¶
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
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 ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.