log

package
v0.0.0-...-2aa3bab Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(skip int) (pc uintptr)

func ErrDebug

func ErrDebug(err error) slog.Level

func ForDefault

func ForDefault(level string, addSource ...bool)

ForDefault 函数用于设置默认日志记录器的级别和是否添加源信息。

参数:

  • level string - 日志级别的字符串表示,例如 "info", "debug" 等。
  • addSource ...bool - 可选参数,用于指定是否添加源信息。如果提供,第一个布尔值将被用于设置是否添加源信息。否则根据 level == debug 判断添加源信息。

func GetPrefix

func GetPrefix(ctx context.Context) slog.Attr

func Level

func Level(logger *slog.Logger, level string)

Level sets the log level

func LevelFromString

func LevelFromString(level string, def slog.Level) slog.Level

LevelFromString parse the level from string, ignore case

  • debug => slog.LevelDebug
  • info, information => slog.LevelInfo
  • warn, warning => slog.LevelWarn
  • error, err => slog.LevelError
  • otherwise slog.LevelInfo

func New

func New(opts *Options) *slog.Logger

func NewHandler

func NewHandler(w io.Writer, opts *Options) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.

func Prefix

func Prefix(ctx context.Context, prefix string) context.Context

func PrefixAttr

func PrefixAttr(prefix string) slog.Attr

func WarnDebug

func WarnDebug(err error) slog.Level

func Writer

func Writer(ctx context.Context) io.Writer

Types

type Options

type Options struct {
	// Enable source code location (Default: false)
	AddSource bool

	// Minimum level to log (Default: slog.LevelInfo)
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// See https://pkg.go.dev/log/slog#HandlerOptions for details.
	ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr

	// Time format (Default: "01/02 15:04:05")
	TimeFormat string

	// Disable color (Default: false)
	NoColor bool
}

Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.

Options can be used as a drop-in replacement for slog.HandlerOptions.

Jump to

Keyboard shortcuts

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