zapKit

package
v3.0.910 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

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

	// OutputFormatJson JSON格式输出
	OutputFormatJson
)

Variables

This section is empty.

Functions

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{})

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

func Debugln added in v3.0.106

func Debugln(args ...interface{})

Debugln

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

func Debugw added in v3.0.106

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

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

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

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

func Infof added in v3.0.106

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

func Infoln added in v3.0.106

func Infoln(args ...interface{})

func Infow added in v3.0.106

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

func MultiCore added in v3.0.106

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

MultiCore

@return != nil

func MultiWriteSyncer added in v3.0.106

func MultiWriteSyncer(ws ...zapcore.WriteSyncer) zapcore.WriteSyncer

MultiWriteSyncer 类似于 io.MultiWriter.

func NewCore added in v3.0.106

func NewCore(enc zapcore.Encoder, ws 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] 日志级别大写且有颜色
(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) 仅有1个输出: 输出到控制台(zapcore.Lock(os.Stdout))
(6) 有 Caller 且 CallerSkip == 0
(7) Development == false,即生产模式
(8) ErrorOutput 使用默认值: zapcore.Lock(os.Stderr)
(9) 输出日志时,不会附带堆栈信息(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 NewSugarLogger added in v3.0.106

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

func NewWriteSyncer added in v3.0.106

func NewWriteSyncer(w io.Writer) zapcore.WriteSyncer

NewWriteSyncer io.Writer => zapcore.WriteSyncer

PS: os.File 结构体实现了 zapcore.WriteSyncer 接口.

func NewWriteSyncerWithLock added in v3.0.910

func NewWriteSyncerWithLock(w io.Writer) zapcore.WriteSyncer

NewWriteSyncerWithLock io.Writer => (线程安全的)zapcore.WriteSyncer

PS: os.File 结构体实现了 zapcore.WriteSyncer 接口.

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 PrintBasicDetails added in v3.0.910

func PrintBasicDetails(loggers ...*zap.SugaredLogger)

func ReplaceGlobalLoggers added in v3.0.910

func ReplaceGlobalLoggers(logger *zap.Logger)

ReplaceGlobalLoggers

PS: 有需要的话,应该在应用初始化时调用此方法,即在最前面设置全局logger.

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 WithFields added in v3.0.106

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

type WrappedLogger added in v3.0.106

type WrappedLogger struct {
	*zap.Logger

	Writers []io.Writer
}

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

适用场景: 使用完后,需要释放对应资源(关闭输出).

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