logger

package
v0.0.0-...-9a49024 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 7 Imported by: 0

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")

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 NewFileCore

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

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"`
}

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