logger

package
v1.7.9 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Level 日志级别

Functions

func NewJSONLogger

func NewJSONLogger(debug bool, w io.Writer, sampler Sampler) *zap.Logger

NewJSONLogger 创建JSON日志

func SetCrashOutput

func SetCrashOutput(f *os.File) error

SetCrashOutput recover panic

func SetLevel

func SetLevel(l string)

SetLevel 设置日志级别 debug/warn/error

func SetupSlog

func SetupSlog(cfg Config) (*slog.Logger, func())

SetupSlog 初始化日志,建议使用 NewDefaultConfig() 创建配置

func WithAttrs added in v1.7.9

func WithAttrs(parent context.Context, attrs ...slog.Attr) context.Context

WithAttrs 批量把字段挂到上下文,后续用该上下文打日志时自动带出这些字段

Types

type Config

type Config struct {
	ServiceID      string  // 服务 ID(可选)
	ServiceName    string  // 服务名称(可选)
	ServiceVersion string  // 服务版本(可选)
	Debug          bool    // 是否开启 debug,日志会同时写终端和文件
	Level          string  // debug/info/warn/error
	Sampler        Sampler // 采样器,用于控制日志写入频率(可选)
	Events         Events  // 日志事件回调,按级别触发(可选),回调同步执行,耗时操作需自行异步化
	FileConfig             // 日志文件配置
}

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig 创建默认配置

func (Config) SetDebug added in v1.4.1

func (c Config) SetDebug(debug bool) Config

SetDebug 设置是否开启 debug,日志会同时写终端和文件

func (Config) SetDir added in v1.4.1

func (c Config) SetDir(dir string) Config

SetDir 设置日志写入目录

func (Config) SetEvents added in v1.7.9

func (c Config) SetEvents(events Events) Config

SetEvents 设置日志事件回调,按级别触发(可选)

func (Config) SetLevel added in v1.4.1

func (c Config) SetLevel(level string) Config

SetLevel 设置日志级别 debug/info/warn/error

func (Config) SetMaxAge added in v1.4.1

func (c Config) SetMaxAge(days int) Config

func (Config) SetRotationTime added in v1.7.0

func (c Config) SetRotationTime(rotationTime time.Duration) Config

func (Config) SetSampler added in v1.4.1

func (c Config) SetSampler(sampler Sampler) Config

SetSampler 设置采样器,用于控制日志写入频率(可选)

func (Config) SetService added in v1.4.1

func (c Config) SetService(id, name, version string) Config

SetService 设置服务信息,可选 - 使用 id 作为服务唯一标识 - 使用 name 作为服务名称 - 使用 version 作为服务版本 id,name,version 空串时,将不记录到日志

type EventFn added in v1.7.9

type EventFn func(ctx context.Context, r slog.Record)

EventFn 日志事件回调,在对应级别的日志写入前同步触发;回调内做耗时操作(如发告警)需自行异步化,否则会拖慢日志写入 回调若需异步留存 record,必须先调用 record.Clone(),否则与后续日志共享底层数据存在竞态

type Events added in v1.7.9

type Events struct {
	OnError EventFn
	OnWarn  EventFn
	OnInfo  EventFn
	OnDebug EventFn
}

Events 按日志级别注册事件回调,未注册的级别零开销

type FileConfig added in v1.7.0

type FileConfig struct {
	Dir          string        // 日志写入目录
	Name         string        // 日志文件名(选填)
	MaxAge       int           // 日志保留时间(天)
	RotationTime time.Duration // 日志分割时间
	MaxSize      int           // 日志文件最大大小(MB)
	Compress     bool          // 是否压缩日志
	MaxBackups   int           // 保留的旧日志归档文件最大数量,超出的自动删除
}

type Option added in v1.7.9

type Option func(*Slog)

func WithEvents added in v1.7.9

func WithEvents(events Events) Option

type Sampler added in v1.2.9

type Sampler struct {
	TickSec    int `command:"时间窗口(秒)"`
	First      int `command:"每个时间窗口内记录的前N条日志"`
	Thereafter int `command:"超过N条后每M条记录一次"`
}

type Slog added in v1.2.12

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

Slog 包装任意 slog.Handler,在写日志前按级别分发事件回调 支持 WithAttrs 批量把字段挂到上下文,后续用该上下文打日志时自动带出这些字段

func New added in v1.7.9

func New(handler slog.Handler, opts ...Option) *Slog

New 创建事件分发 Handler,事件回调通过 Option 注入,未注入时零回调开销

func (*Slog) Enabled added in v1.7.9

func (s *Slog) Enabled(ctx context.Context, level slog.Level) bool

Enabled 委托底层 handler 判断级别,保证事件分发与实际写日志的级别过滤一致

func (*Slog) Handle added in v1.2.12

func (s *Slog) Handle(ctx context.Context, record slog.Record) error

Handle 先按级别分发事件回调,再交给底层 handler 写日志;事件在底层 handler 之前触发,被采样丢弃的日志同样会触发回调

func (*Slog) WithAttrs added in v1.7.9

func (s *Slog) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs 自行包装底层 handler,保证 log.With() 之后事件回调仍然生效

func (*Slog) WithGroup added in v1.7.9

func (s *Slog) WithGroup(name string) slog.Handler

WithGroup 包装原因同 WithAttrs

Jump to

Keyboard shortcuts

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