logger

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 10 Imported by: 0

README

logger

logger 是基于 log/slog 的轻量封装。它不试图接管全局日志体系,只提供统一格式、trace 上下文字段注入和运行时开关。

设计原则

  • 默认输出 JSON,适合直接进采集链路。
  • source 字段只保留文件名,避免冗长绝对路径污染日志面。
  • trace_id / span_id 仅在上下文里有有效 span 时注入。
  • 不强制要求非 nil context;日志在没有上下文时仍可安全输出。
  • With / WithGroup 共享启用状态和级别状态,便于统一开关。

快速开始

log := logger.New(
    logger.WithLevel("debug"),
    logger.WithFormat("json"),
    logger.WithAddSource(true),
)

log.Info(context.Background(), "service started", "service", "api")

httpLog := log.With("service", "api").WithGroup("http")
httpLog.Debug(context.Background(), "request completed", "status", 200)

slogLogger := log.GetSlog()
slogLogger.Info("plain slog is still available")

API 摘要

func New(opts ...Option) *Logger
func WithLevel(string) Option
func WithFormat(string) Option
func WithAddSource(bool) Option
func WithOutput(io.Writer) Option

func (l *Logger) Toggle(bool)
func (l *Logger) IsEnabled() bool
func (l *Logger) Debug(context.Context, string, ...any)
func (l *Logger) Info(context.Context, string, ...any)
func (l *Logger) Warn(context.Context, string, ...any)
func (l *Logger) Error(context.Context, string, ...any)
func (l *Logger) With(...any) *Logger
func (l *Logger) WithGroup(string) *Logger
func (l *Logger) GetSlog() *slog.Logger

默认行为

  • 默认级别是 info
  • 默认格式是 json
  • 默认开启 source
  • 传入未知级别会回落到 info
  • 传入未知格式会回落到 text handler

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

func New

func New(opts ...Option) *Logger

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, args ...any)

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string, args ...any)

func (*Logger) GetSlog

func (l *Logger) GetSlog() *slog.Logger

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, args ...any)

func (*Logger) IsEnabled

func (l *Logger) IsEnabled() bool

func (*Logger) Toggle

func (l *Logger) Toggle(enable bool)

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, args ...any)

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

type Option

type Option func(*options)

func WithAddSource

func WithAddSource(add bool) Option

func WithFormat

func WithFormat(format string) Option

func WithLevel

func WithLevel(level string) Option

func WithOutput

func WithOutput(w io.Writer) Option

Jump to

Keyboard shortcuts

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