logging

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logging provides structured logging with slog. Package logging 提供基于 slog 的结构化日志。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr = slog.Attr

Attr is a short alias for slog.Attr. Attr 是 slog.Attr 的简短别名。

func Any

func Any(key string, value any) Attr

func Bool

func Bool(key string, value bool) Attr

func Float64

func Float64(key string, value float64) Attr

func Int

func Int(key string, value int) Attr

func Int64

func Int64(key string, value int64) Attr

func Str

func Str(key, value string) Attr

func Time

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

type Format

type Format string

Format controls log output format. Format 控制日志输出格式。

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

func ParseFormat

func ParseFormat(raw string) (Format, error)

ParseFormat parses a log format string. ParseFormat 解析日志格式字符串。

type Level

type Level int

Level represents log verbosity. Level 表示日志级别。

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

func ParseLevel

func ParseLevel(raw string) (Level, error)

ParseLevel parses a log level string. ParseLevel 解析日志级别字符串。

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	// Debug logs a message with an optional error value.
	// Debug 记录消息并可附带错误信息。
	Debug(msg string, err error, attrs ...Attr)
	// Info logs a message with an optional error value.
	// Info 记录消息并可附带错误信息。
	Info(msg string, err error, attrs ...Attr)
	// Warn logs a message with an optional error value.
	// Warn 记录消息并可附带错误信息。
	Warn(msg string, err error, attrs ...Attr)
	// Error logs a message with an optional error value.
	// Error 记录消息并可附带错误信息。
	Error(msg string, err error, attrs ...Attr)
	With(attrs ...Attr) Logger
	Enabled(level Level) bool
	Slog() *slog.Logger
}

Logger provides structured logging. Logger 提供结构化日志。

func New

func New(opts Options) Logger

New creates a logger with options. New 根据配置创建日志器。

Example / 示例:

logger := logging.New(logging.Options{Level: logging.LevelInfo, Format: logging.FormatJSON})

type Options

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

Options configures logger. Options 配置日志器。

Jump to

Keyboard shortcuts

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