zapKit

package
v3.1.112 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OutputFormatConsole 人类可读的多行输出
	OutputFormatConsole outputFormat = iota

	// OutputFormatJson JSON格式输出
	OutputFormatJson
)

Variables

View Source
var (
	LockedWriteSyncerStdout = ioKit.LockedWriteSyncerStdout

	LockedWriteSyncerStderr = ioKit.LockedWriteSyncerStderr
)
View Source
var (
	// NewWriteSyncer io.Writer => (并发不安全的)zapcore.WriteSyncer
	/*
	   PS:
	   (1) os.File 结构体实现了 zapcore.WriteSyncer 接口;
	   (2) zapcore.WriteSyncer 接口是 io.Writer 接口的子类.
	*/
	NewWriteSyncer func(w io.Writer) zapcore.WriteSyncer = ioKit.NewWriteSyncer

	// NewLockedWriteSyncer io.Writer => (并发安全的)zapcore.WriteSyncer
	/*
	   PS:
	   (1) os.File 结构体实现了 zapcore.WriteSyncer 接口;
	   (2) zapcore.WriteSyncer 接口是 io.Writer 接口的子类.
	*/
	NewLockedWriteSyncer func(w io.Writer) zapcore.WriteSyncer = ioKit.NewLockedWriteSyncer

	// MultiWriteSyncer 类似于 io.MultiWriter.
	MultiWriteSyncer func(ws ...zapcore.WriteSyncer) zapcore.WriteSyncer = ioKit.MultiWriteSyncer
)

Functions

func CanLoggerPrintSpecifiedLevel added in v3.0.106

func CanLoggerPrintSpecifiedLevel(logger *zap.Logger, lv zapcore.Level) bool

CanLoggerPrintSpecifiedLevel logger能否打印 指定级别lv 的日志?

func DPanic added in v3.0.106

func DPanic(msg string, fields ...zap.Field)

func DPanicf added in v3.0.106

func DPanicf(template string, args ...interface{})

func DPanicln added in v3.0.106

func DPanicln(args ...interface{})

func DPanicw added in v3.0.106

func DPanicw(msg string, keysAndValues ...interface{})

func Debug added in v3.0.106

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

func Debugf added in v3.0.106

func Debugf(template string, args ...interface{})

func Debugln added in v3.0.106

func Debugln(args ...interface{})

func Debugw added in v3.0.106

func Debugw(msg string, keysAndValues ...interface{})

func Error added in v3.0.106

func Error(msg string, fields ...zap.Field)

func Errorf added in v3.0.106

func Errorf(template string, args ...interface{})

func Errorln added in v3.0.106

func Errorln(args ...interface{})

func Errorw added in v3.0.106

func Errorw(msg string, keysAndValues ...interface{})

func Exit added in v3.0.106

func Exit(codes ...int)

Exit 退出程序(应用).

PS: 无论是在main程还是子程中,只要调用os.Exit(),程序就会终止.

@param codes (1) 可以不传(默认code: 1)

(2) 为了可移植性,推荐范围: [0, 125]
	For portability, the status code should be in the range [0, 125].

func Fatal added in v3.0.106

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

func Fatalf added in v3.0.106

func Fatalf(template string, args ...interface{})

func Fatalln added in v3.0.106

func Fatalln(args ...interface{})

func Fatalw added in v3.0.106

func Fatalw(msg string, keysAndValues ...interface{})

func Info added in v3.0.106

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

Info

@param fields 输出循序与 传参fields 顺序一致(并不会按字母排序)

func Infof added in v3.0.106

func Infof(template string, args ...interface{})

Infof 格式化输出的信息日志,类似于 fmt.Printf ,可以使用格式化字符串.

func Infoln added in v3.0.106

func Infoln(args ...interface{})

Infoln

PS: Spaces are always added between arguments.(传参间会加上" ")

func Infow added in v3.0.106

func Infow(msg string, keysAndValues ...interface{})

Infow 结构化输出的信息日志,使用键值对的方式输出,更加适合记录结构化数据.

@param keysAndValues e.g. "key", "value", "flag", true

func L added in v3.0.106

func L() *zap.Logger

func MultiCore added in v3.0.106

func MultiCore(cores ...zapcore.Core) zapcore.Core

MultiCore

@return != nil

func NewCore added in v3.0.106

func NewCore(enc zapcore.Encoder, writeSyncer zapcore.WriteSyncer, levelEnabler zapcore.LevelEnabler, initialFields ...zapcore.Field) zapcore.Core

NewCore

@param enc 不能为nil @param ws 可以为nil(默认输出到控制台) @param levelEnabler (1) 不能为nil

(2) 可以是多种:
	(a) zapcore.Level 类型(级别 >= 此值的才会输出)
		e.g.
		zapcore.DebugLevel
		zapcore.InfoLevel
		zapcore.WarnLevel
		zapcore.ErrorType
		zapcore.PanicLevel
		zapcore.DPanicLevel
		zapcore.FatalLevel
		zapcore.InvalidLevel
	(b) zap.LevelEnablerFunc 类型(更加地自定义)
		e.g.
		// 创建错误日志级别的核心
		errorLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
			return level >= zapcore.ErrorLevel
		})

@param initialFields 可以不传

func NewEncoder added in v3.0.106

func NewEncoder(options ...EncoderOption) zapcore.Encoder

NewEncoder

默认(不传参):

(1) [Encoder] 人类可读的多行输出
(2) [Encoder] 时间格式: "2024-06-28T09:15:16.176+0800"
(3) [Encoder] 日志级别大写且有颜色(color)
(4) [Encoder] Message字段无前缀

func NewIncreaseLevelCore added in v3.0.106

func NewIncreaseLevelCore(core zapcore.Core, levelEnabler zapcore.LevelEnabler) (zapcore.Core, error)

func NewLazyWith added in v3.0.106

func NewLazyWith(core zapcore.Core, fields []zapcore.Field) zapcore.Core

func NewLogger

func NewLogger(core zapcore.Core, options ...LoggerOption) (logger *zap.Logger)

NewLogger

PS: (1) 自定义字段(Field),创建 core 和 logger 时都能添加.

@param core 可以为nil @param options 可以不传

e.g. case: core传nil,options不传

(1) [Encoder] 人类可读的多行输出
(2) [Encoder] 时间格式: "2024-06-28T09:15:16.176+0800"
(3) [Encoder] 日志级别大写且有颜色
(4) [Encoder] Message字段无前缀
(5) [Core] 仅有1个输出: 输出到控制台(并发安全地输出到os.Stdout)
(6) [Core] 仅有1个输出: 日志级别(level)为 DEBUG
(6) [Logger] 有 Caller 且 CallerSkip == 0
(7) [Logger] Development == false,即生产模式
(8) [Logger] ErrorOutput 使用默认值: 并发安全地输出到os.Stderr
(9) [Logger] ERROR及以上级别 的日志输出,会附带堆栈信息(stack trace)

func NewNopCore added in v3.0.106

func NewNopCore() zapcore.Core

func NewNopLogger added in v3.0.106

func NewNopLogger() *zap.Logger

NewNopLogger 不进行实际日志记录操作.

func NewNopSugaredLogger added in v3.0.106

func NewNopSugaredLogger() *zap.SugaredLogger

NewNopSugaredLogger 不进行实际日志记录操作.

func NewPrefixEncoder added in v3.0.106

func NewPrefixEncoder(encoder zapcore.Encoder, prefix string) zapcore.Encoder

NewPrefixEncoder 会给msg字段加上前缀.

@param encoder 不能为nil @return 可能是传参encoder

func NewStdLogger added in v3.0.106

func NewStdLogger(l *zap.Logger) *log.Logger

NewStdLogger *zap.Logger => *log.Logger

func NewStdLoggerWithLevel added in v3.0.106

func NewStdLoggerWithLevel(l *zap.Logger, level zapcore.Level) (*log.Logger, error)

NewStdLoggerWithLevel *zap.Logger => *log.Logger

func NewSugarLogger added in v3.0.106

func NewSugarLogger(core zapcore.Core, options ...LoggerOption) *zap.SugaredLogger

func Panic added in v3.0.106

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

func Panicf added in v3.0.106

func Panicf(template string, args ...interface{})

func Panicln added in v3.0.106

func Panicln(args ...interface{})

func Panicw added in v3.0.106

func Panicw(msg string, keysAndValues ...interface{})

func RedirectStdLog added in v3.0.106

func RedirectStdLog(l *zap.Logger) func()

RedirectStdLog 重定向 log标准库 的输出

func RedirectStdLogWithLevel added in v3.0.106

func RedirectStdLogWithLevel(l *zap.Logger, level zapcore.Level) (func(), error)

func RegisterExitHandler added in v3.0.106

func RegisterExitHandler(handlers ...func())

func RegisterParallelExitHandler added in v3.0.106

func RegisterParallelExitHandler(handlers ...func())

func RunExitHandlers added in v3.0.106

func RunExitHandlers()

func S added in v3.0.106

func S() *zap.SugaredLogger

func SetDefaultLevel added in v3.1.112

func SetDefaultLevel(level zapcore.Level)

func SetExitTimeout added in v3.0.106

func SetExitTimeout(d time.Duration)

SetExitTimeout 执行所有exit handler的超时时间.

func StringToLevel added in v3.0.106

func StringToLevel(str string) (zapcore.Level, error)

StringToLevel

PS: 默认 DEBUG 级别.

func Sync added in v3.0.106

func Sync()

func Warn added in v3.0.106

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

func Warnf added in v3.0.106

func Warnf(template string, args ...interface{})

func Warnln added in v3.0.106

func Warnln(args ...interface{})

func Warnw added in v3.0.106

func Warnw(msg string, keysAndValues ...interface{})

Types

type EncoderOption added in v3.0.106

type EncoderOption func(opts *encoderOptions)

func WithEncoderEncodeLevel added in v3.0.106

func WithEncoderEncodeLevel(encodeLevel zapcore.LevelEncoder) EncoderOption

func WithEncoderEncodeTime added in v3.0.106

func WithEncoderEncodeTime(encodeTime zapcore.TimeEncoder) EncoderOption

func WithEncoderMessagePrefix added in v3.0.106

func WithEncoderMessagePrefix(prefix string) EncoderOption

func WithEncoderOutputFormatConsole added in v3.0.106

func WithEncoderOutputFormatConsole() EncoderOption

func WithEncoderOutputFormatJson added in v3.0.106

func WithEncoderOutputFormatJson() EncoderOption

type LoggerOption added in v3.0.106

type LoggerOption func(opts *loggerOptions)

func WithAddStacktrace added in v3.0.106

func WithAddStacktrace(levelEnabler zapcore.LevelEnabler) LoggerOption

func WithCaller added in v3.0.106

func WithCaller(caller bool) LoggerOption

func WithCallerSkip added in v3.0.106

func WithCallerSkip(skip int) LoggerOption

func WithClock added in v3.0.106

func WithClock(clock zapcore.Clock) LoggerOption

func WithDevelopment added in v3.0.106

func WithDevelopment(flag bool) LoggerOption

func WithErrorOutput added in v3.0.106

func WithErrorOutput(writeSyncer zapcore.WriteSyncer) LoggerOption

func WithFatalHook deprecated added in v3.0.106

func WithFatalHook(hook zapcore.CheckWriteHook) LoggerOption

WithFatalHook

Deprecated: 不推荐替换默认的fatal hook.一定要替换的换,建议使用 appKit.Exit 而非 os.Exit 退出应用.

func WithFields added in v3.0.106

func WithFields(fields ...zapcore.Field) LoggerOption

func WithPanicHook added in v3.0.106

func WithPanicHook(hook zapcore.CheckWriteHook) LoggerOption

type WrappedLogger added in v3.0.106

type WrappedLogger struct {
	*zap.Logger

	Writers []io.Writer
}

func NewFileLogger added in v3.0.106

func NewFileLogger(filePath string, prefix string, level zapcore.Level, loggerOptions ...LoggerOption) (*WrappedLogger, error)

NewFileLogger

PS: 仅输出到文件.

@param filePath 日志文件路径

func WrapLogger added in v3.0.106

func WrapLogger(logger *zap.Logger, writers ...io.Writer) *WrappedLogger

WrapLogger

PS: (1) 适用场景: 使用完后,需要释放对应资源(关闭输出). (2) 不要调用返回值的 Sugar()!!!

func (*WrappedLogger) Close added in v3.0.106

func (l *WrappedLogger) Close() (err error)

Close 释放资源.

!!!: 要注意并发问题,调用本方法后,不要再通过此logger输出了.

type WrappedSugaredLogger added in v3.0.106

type WrappedSugaredLogger struct {
	*zap.SugaredLogger

	Writers []io.Writer
}

func WrapSugarLogger added in v3.0.106

func WrapSugarLogger(sugaredLogger *zap.SugaredLogger, writers ...io.Writer) *WrappedSugaredLogger

WrapSugarLogger

PS: (1) 适用场景: 使用完后,需要释放对应资源(关闭输出). (2) 不要调用返回值的 Desugar()!!!

func (*WrappedSugaredLogger) Close added in v3.0.106

func (sl *WrappedSugaredLogger) Close() (err error)

Close 释放资源.

!!!: 要注意并发问题,调用本方法后,不要再通过此logger输出了.

Jump to

Keyboard shortcuts

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