log

package
v0.0.0-...-384b7dd Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

基于zap zapcore进行二次封装,扩展的log日志库,使用支持的弱类型的sugar操作

Index

Constants

View Source
const (
	DebugLevel = zapcore.DebugLevel
	InfoLevel  = zapcore.InfoLevel
	WarnLevel  = zapcore.WarnLevel
	ErrorLevel = zapcore.ErrorLevel
	PanicLevel = zapcore.PanicLevel
	FatalLevel = zapcore.FatalLevel
)

Variables

View Source
var (
	Skip        = zap.Skip
	Binary      = zap.Binary
	Bool        = zap.Bool
	Boolp       = zap.Boolp
	ByteString  = zap.ByteString
	Complex128  = zap.Complex128
	Complex128p = zap.Complex128p
	Complex64   = zap.Complex64
	Complex64p  = zap.Complex64p
	Float64     = zap.Float64
	Float64p    = zap.Float64p
	Float32     = zap.Float32
	Float32p    = zap.Float32p
	Int         = zap.Int
	Intp        = zap.Intp
	Int64       = zap.Int64
	Int64p      = zap.Int64p
	Int32       = zap.Int32
	Int32p      = zap.Int32p
	Int16       = zap.Int16
	Int16p      = zap.Int16p
	Int8        = zap.Int8
	Int8p       = zap.Int8p
	String      = zap.String
	Stringp     = zap.Stringp
	Uint        = zap.Uint
	Uintp       = zap.Uintp
	Uint64      = zap.Uint64
	Uint64p     = zap.Uint64p
	Uint32      = zap.Uint32
	Uint32p     = zap.Uint32p
	Uint16      = zap.Uint16
	Uint16p     = zap.Uint16p
	Uint8       = zap.Uint8
	Uint8p      = zap.Uint8p
	Uintptr     = zap.Uintptr
	Uintptrp    = zap.Uintptrp
	Reflect     = zap.Reflect
	Namespace   = zap.Namespace
	Stringer    = zap.Stringer
	Time        = zap.Time
	Timep       = zap.Timep
	Stack       = zap.Stack
	StackSkip   = zap.StackSkip
	Duration    = zap.Duration
	Durationp   = zap.Durationp
	Object      = zap.Object
	Inline      = zap.Inline
	Error       = zap.Error
	Any         = zap.Any
)
View Source
var (
	WrapCore      = zap.WrapCore
	Hooks         = zap.Hooks
	Fields        = zap.Fields
	ErrorOutput   = zap.ErrorOutput
	Development   = zap.Development
	AddCaller     = zap.AddCaller
	WithCaller    = zap.WithCaller
	AddCallerSkip = zap.AddCallerSkip
	AddStacktrace = zap.AddStacktrace
	IncreaseLevel = zap.IncreaseLevel
	WithFatalHook = zap.WithFatalHook
	WithClock     = zap.WithClock
)

Functions

func Debug

func Debug(msg string, fields ...Field)

func Debugf

func Debugf(format string, args ...any)

func Debugw

func Debugw(msg string, keysAndValues ...any)

func Enabled

func Enabled() bool

func Errorf

func Errorf(format string, args ...any)

func Errors

func Errors(msg string, fields ...Field)

func Errorw

func Errorw(msg string, keysAndValues ...any)

func Fatal

func Fatal(msg string, fields ...Field)

func Fatalf

func Fatalf(format string, args ...any)

func Fatalw

func Fatalw(msg string, keysAndValues ...any)

func Info

func Info(msg string, fields ...Field)

func Infof

func Infof(format string, args ...any)

func Infow

func Infow(msg string, keysAndValues ...any)

func Named

func Named(name string)

func NewProductionRotateBySize

func NewProductionRotateBySize(filename string) io.Writer

func NewProductionRotateByTime

func NewProductionRotateByTime(filename string) io.Writer

func NewRotateBySize

func NewRotateBySize(cfg *RotateConfig) io.Writer

func NewRotateByTime

func NewRotateByTime(cfg *RotateConfig) io.Writer

func OpenLogFile

func OpenLogFile(file string) io.Writer

func Panic

func Panic(msg string, fields ...Field)

func Panicf

func Panicf(format string, args ...any)

func Panicw

func Panicw(msg string, keysAndValues ...any)

func ReplaceDefault

func ReplaceDefault(l Logger)

func SetLevel

func SetLevel(level Level)

func Sync

func Sync()

func Warn

func Warn(msg string, fields ...Field)

func Warnf

func Warnf(format string, args ...any)

func Warnw

func Warnw(msg string, keysAndValues ...any)

func WithContext

func WithContext(ctx context.Context) context.Context

func ZapLogger

func ZapLogger() *zapLogger

Types

type Field

type Field = zap.Field

type InfoLogger

type InfoLogger interface {
	Info(msg string, fields ...Field)
	Infof(format string, args ...any)
	Infow(msg string, keysAndValues ...any)

	Enabled() bool
}

InfoLogger 作为基础的日志接口,仅提供info级别的日志接口,

func V

func V(level Level) InfoLogger

type Level

type Level = zapcore.Level

type LevelEnablerFunc

type LevelEnablerFunc func(lvl Level) bool

type LogEncoder

type LogEncoder string
const (
	ConsoleEncoder LogEncoder = "Console"
	JsonEncoder    LogEncoder = "Json"
)

type Logger

type Logger interface {
	InfoLogger
	Debug(msg string, fields ...Field)
	Debugf(format string, args ...any)
	Debugw(msg string, keysAndValues ...any)
	Warn(msg string, fields ...Field)
	Warnf(format string, args ...any)
	Warnw(msg string, keysAndValues ...any)
	Error(msg string, fields ...Field)
	Errorf(format string, args ...any)
	Errorw(msg string, keysAndValues ...any)
	Panic(msg string, fields ...Field)
	Panicf(format string, args ...any)
	Panicw(msg string, keysAndValues ...any)
	Fatal(msg string, fields ...Field)
	Fatalf(format string, args ...any)
	Fatalw(msg string, keysAndValues ...any)

	// V 返回一个 InfoLogger 允许通过传入的日志级别灵活指定 InfoLogger.
	V(level Level) InfoLogger

	// WithValues 添加一个 key-value 键值对到 logger 中.
	WithValues(keysAndvalues ...any) Logger

	// WithName 为 logger 添加一个名称,方便在日志中区分日志来源.
	WithName(name string) Logger

	// WithContext
	WithContext(ctx context.Context) context.Context

	// L 从 context 中获取对应的值添加到 logger 中
	L(ctx context.Context, keys ...string) Logger

	Sync()
}

Logger 完整的日志接口

func FromContext

func FromContext(ctx context.Context) Logger

func L

func L(ctx context.Context, keys ...string) Logger

func NewLogger

func NewLogger(out Output, lvl Level, encoder LogEncoder, zOpts ...ZapOption) Logger

func NewTeeLogger

func NewTeeLogger(tees []TeeOption, encoder LogEncoder, opts ...ZapOption) Logger

func WithName

func WithName(name string) Logger

func WithValues

func WithValues(keysAndvalues ...any) Logger

type Output

type Output func() io.Writer

type RotateConfig

type RotateConfig struct {
	// 共用配置
	Filename string // 完整配置名
	MaxAge   int    // 保留旧日志文件最大时间

	// 按时间轮转配置
	RotationTime time.Duration

	// 按大小轮转配置
	MaxSize    int  // 日志文件最大大小
	MaxBackups int  // 保留日志文件的最大数量
	Compress   bool // 是否对日志文件进行压缩
	LocalTime  bool // 是否使用本地时间
}

日志轮转功能 借用file-rotatelogs lumberjack实现

func NewProductionRotateConfig

func NewProductionRotateConfig(filename string) *RotateConfig

type TeeOption

type TeeOption struct {
	Output io.Writer
	LevelEnablerFunc
}

type ZapOption

type ZapOption = zap.Option

Directories

Path Synopsis
demolog
slog/test command
zlog/test command

Jump to

Keyboard shortcuts

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