logging

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 12 Imported by: 0

README

logging

  • New returns *slog.Logger.
  • Helper wraps *slog.Logger.
  • Public package boundaries use *slog.Logger.
  • App code may use Helper.

Pass a non-nil context to each Helper logging call and to Enabled:

logger := logging.NewHelper(logging.New(logging.Options{
	Level:     logging.LevelInfo,
	AddSource: true,
}))
logger = logger.With(logging.String("service", "accounts"))
logger.Info(ctx, "user saved", nil, logging.String("user_id", userID))
logger.Error(ctx, "save user failed", err, logging.String("user_id", userID))

Use the request context in handlers and context.Background() for startup logs. The context reaches the underlying handler for filtering and output. A non-nil error is appended as the error attribute. With keeps its own attribute slice.

AddSource includes the application call site in both text and JSON output. Text output uses source=file:line; JSON output uses slog's source object.

Documentation

Overview

Package logging provides slog-based logging. Package logging 提供基于 slog 的日志。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(key string, value any) slog.Attr

func Bool

func Bool(key string, value bool) slog.Attr

func Float64

func Float64(key string, value float64) slog.Attr

func Int

func Int(key string, value int) slog.Attr

func Int64

func Int64(key string, value int64) slog.Attr

func New

func New(opts Options) *slog.Logger

New returns a base logger for APIs that accept *slog.Logger. Call New(Options{...}) to build the base logger. New 返回供接收 *slog.Logger 的 API 使用的底层 logger。 调用 New(Options{...}) 构建底层 logger。

func String added in v0.1.8

func String(key, value string) slog.Attr

func Time

func Time(key string, value time.Time) slog.Attr

Types

type Format

type Format string

Format is the log output format. Format 表示日志输出格式。

const (
	FormatText Format = "text"
	FormatJSON Format = "json"
)

func ParseFormat

func ParseFormat(raw string) (Format, error)

ParseFormat parses `text` or `json`. ParseFormat 解析 `text` 或 `json`。

type Helper added in v0.1.5

type Helper struct {
	// contains filtered or unexported fields
}

Helper wraps *slog.Logger with Error(ctx, msg, err, attrs...) style calls. Logging methods and Enabled require a non-nil context. Helper 用 Error(ctx, msg, err, attrs...) 形式包装 *slog.Logger。 日志方法和 Enabled 要求非 nil context。

func NewHelper added in v0.1.5

func NewHelper(logger *slog.Logger) *Helper

NewHelper returns a Helper for a base logger. Call NewHelper(base).Info(...) or NewHelper(base).Error(...). logger must not be nil. NewHelper 返回基于底层 logger 的 Helper。 调用 NewHelper(base).Info(...) 或 NewHelper(base).Error(...)。 logger 不能为空。

func (*Helper) Debug added in v0.1.5

func (h *Helper) Debug(ctx context.Context, msg string, err error, attrs ...slog.Attr)

Debug logs msg, err, and attrs at debug level. Debug 以 debug 级别记录 msg、err 和 attrs。

func (*Helper) Enabled added in v0.1.5

func (h *Helper) Enabled(ctx context.Context, level Level) bool

Enabled reports whether a level is enabled. Call Enabled before building expensive attrs. Enabled 返回某个级别是否启用。 在构造代价高的 attrs 前调用 Enabled。

func (*Helper) Error added in v0.1.5

func (h *Helper) Error(ctx context.Context, msg string, err error, attrs ...slog.Attr)

Error logs msg, err, and attrs at error level. Error 以 error 级别记录 msg、err 和 attrs。

func (*Helper) Info added in v0.1.5

func (h *Helper) Info(ctx context.Context, msg string, err error, attrs ...slog.Attr)

Info logs msg, err, and attrs at info level. Info 以 info 级别记录 msg、err 和 attrs。

func (*Helper) Logger added in v0.1.5

func (h *Helper) Logger() *slog.Logger

Logger returns the base logger. Use Logger() where an API requires *slog.Logger. Logger 返回底层 logger。 在某个 API 需要 *slog.Logger 时调用 Logger()。

func (*Helper) Warn added in v0.1.5

func (h *Helper) Warn(ctx context.Context, msg string, err error, attrs ...slog.Attr)

Warn logs msg, err, and attrs at warn level. Warn 以 warn 级别记录 msg、err 和 attrs。

func (*Helper) With added in v0.1.5

func (h *Helper) With(attrs ...slog.Attr) *Helper

With returns a child helper with bound attrs. Call h.With(attrs...).Info(...) to reuse attrs across calls. With 返回绑定 attrs 的子 helper。 调用 h.With(attrs...).Info(...) 复用 attrs。

type Level

type Level int

Level is the log verbosity. Level 表示日志级别。

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

func ParseLevel

func ParseLevel(raw string) (Level, error)

ParseLevel parses `debug`, `info`, `warn`, or `error`. ParseLevel 解析 `debug`、`info`、`warn` 或 `error`。

func (Level) SlogLevel added in v0.1.5

func (l Level) SlogLevel() slog.Level

SlogLevel returns the corresponding slog level. SlogLevel 返回对应的 slog 级别。

func (Level) String

func (l Level) String() string

type Options

type Options struct {
	Level      Level
	Format     Format
	Writer     io.Writer
	TimeFormat string
	AddSource  bool
}

Options configures New. Options 配置 New。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL