logger

package
v0.0.0-...-547c525 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 7 Imported by: 5 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

日志组件

封装zap作为日志组件

使用

logger.Default().Info("info")
logger.With(zap.String("key", "value")).Info("info")

// 把日志实例放入context中
ctx:=logger.ContextWith(ctx, zap.String("key", "value"))
ctx:=logger.ContextWithLogger(ctx, logger.With(zap.String("key", "value")))

// 从context中取出日志实例使用
logger.Ctx(ctx).Info("info")
logger.CtxWith(ctx, zap.String("key", "value")).Info("info")

写文件与轮转

logFile.enable 打开后按 filename / errorFilename 写文件,由 lumberjack 轮转,两个文件共用下面的轮转参数。 每项零值取默认值,负数表示不限。

logger:
  logFile:
    enable: true
    filename: ./runtime/logs/app.log
    errorFilename: ./runtime/logs/app.err.log
    maxSize: 2       # 单个文件大小上限(MB),默认 2
    maxBackups: 10   # 保留的已轮转文件个数,默认 10
    maxAge: 30       # 已轮转文件保留天数,默认 30
    compress: false  # 是否 gzip 压缩已轮转的文件,默认 false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ctx

func Ctx(ctx context.Context) *zap.Logger

Ctx 从上下文中获取日志实例

func Default

func Default() *zap.Logger

Default 默认日志,尽量不要使用,会丢失上下文信息

func Logger

func Logger(ctx context.Context, config *configs.Config) error

Logger 日志组件,核心组件,必须提前注册

func New

func New(opt ...Option) (*zap.Logger, error)

func NewConsole

func NewConsole(w io.Writer, level string) *zap.Logger

NewConsole creates a logger suitable for short-lived CLI commands. Unlike the application logger, it does not require a config file and omits timestamps and caller information to keep terminal output concise.

func NewFileCore

func NewFileCore(level zapcore.Level, filename string) zapcore.Core

NewFileCore 按默认轮转参数创建写文件的日志 core。

func NewRotateFileCore

func NewRotateFileCore(level zapcore.Level, filename string, cfg LogFileConfig) zapcore.Core

NewRotateFileCore 按 cfg 里的轮转参数创建写文件的日志 core,cfg 中的文件名字段不参与。

func RegistryInitLogger

func RegistryInitLogger(f InitLogger)

func SetLogger

func SetLogger(l *zap.Logger)

SetLogger 设置全局日志实例

func ToLevel

func ToLevel(level string) zapcore.Level

func With

func With(fields ...zap.Field) *zap.Logger

With 默认日志添加字段,尽量不要使用,会丢失上下文信息

func WithContextField

func WithContextField(ctx context.Context, fields ...zap.Field) context.Context

WithContextField 取出context中的日志实例并添加字段存入上下文 在想为后续操作添加字段时使用 logger.WithContextField(ctx, zap.String("key", "value"))

func WithContextLogger

func WithContextLogger(ctx context.Context, logger *zap.Logger) context.Context

WithContextLogger 将日志实例存入上下文 在想为后续操作指定日志实例时使用 logger.WithContextLogger(ctx, logger.With(zap.String("key", "value")))

Types

type Config

type Config struct {
	Level          string
	DisableConsole bool          `yaml:"disableConsole"`
	LogFile        LogFileConfig `yaml:"logFile"`
}

type CtxLogger

type CtxLogger struct {
	*zap.Logger
	// contains filtered or unexported fields
}

func NewCtxLogger

func NewCtxLogger(logger *zap.Logger) *CtxLogger

func (*CtxLogger) Ctx

func (c *CtxLogger) Ctx(ctx context.Context) *CtxLogger

func (*CtxLogger) With

func (c *CtxLogger) With(fields ...zap.Field) *CtxLogger

type InitLogger

type InitLogger func(ctx context.Context, config *configs.Config, loggerConfig *Config) ([]Option, error)

type LogFileConfig

type LogFileConfig struct {
	Enable        bool
	Filename      string
	ErrorFilename string `yaml:"errorFilename"`
	// 以下是 lumberjack 的轮转参数,Filename 与 ErrorFilename 共用。
	// 零值沿用默认值,不配的应用行为与改动前一致。
	// MaxSize 单个文件的大小上限(MB),默认 2
	MaxSize int `yaml:"maxSize"`
	// MaxBackups 保留的已轮转文件个数,默认 10;负数表示不按个数清理
	MaxBackups int `yaml:"maxBackups"`
	// MaxAge 已轮转文件的保留天数,默认 30;负数表示不按天数清理
	MaxAge int `yaml:"maxAge"`
	// Compress 是否把已轮转的文件 gzip 压缩
	Compress bool `yaml:"compress"`
}

type Option

type Option func(*Options)

func AppendCore

func AppendCore(core ...zapcore.Core) Option

func Level

func Level(level string) Option

func WithWriter

func WithWriter(w io.Writer) Option

type Options

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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