logger

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 13 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 WithAttr added in v1.2.12

func WithAttr(parent context.Context, attr slog.Attr) context.Context

WithAttr 使用此函数创建的上下文,当应用在 slog 上下文时,会自动记录存在 context 中的参数

Types

type Config

type Config struct {
	Dir            string        // 日志写入目录
	ServiceID      string        // 服务 ID(可选)
	ServiceName    string        // 服务名称(可选)
	ServiceVersion string        // 服务版本(可选)
	Debug          bool          // 是否开启 debug,日志会同时写终端和文件
	MaxAge         time.Duration // 日志保留时间
	RotationTime   time.Duration // 日志分割时间
	RotationSize   int64         // 日志分割大小,单位字节
	Level          string        // debug/info/warn/error
	Sampler        Sampler       // 采样器,用于控制日志写入频率(可选)
}

Config ....

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig 创建默认配置 默认行为 - 最大 50MB 的文件即创建新文件 - 12 小时分割一个新的日志文件 - 仅保留最近 7 天的文件

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) SetLevel added in v1.4.1

func (c Config) SetLevel(level string) Config

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

func (Config) SetMaxAge added in v1.4.1

func (c Config) SetMaxAge(maxAge time.Duration) Config

SetMaxAge 设置日志保留时间

func (Config) SetRotation added in v1.4.1

func (c Config) SetRotation(b int64, duration time.Duration) Config

SetRotation 注意单位是 b Deprecated: 建议使用 SetRotationKB

func (Config) SetRotationKB added in v1.5.1

func (c Config) SetRotationKB(kb int64, duration time.Duration) Config

SetRotationMB 按照 MB 分割日志文件,time 为分割时间间隔

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 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 {
	*zapslog.Handler
}

func (*Slog) Handle added in v1.2.12

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

Jump to

Keyboard shortcuts

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