Documentation ¶
Index ¶
- Constants
- func DPanic(msg string, fields ...zap.Field)
- func DPanicf(template string, args ...interface{})
- func DPanicln(args ...interface{})
- func DPanicw(msg string, keysAndValues ...interface{})
- func Debug(msg string, fields ...zap.Field)
- func Debugf(template string, args ...interface{})
- func Debugln(args ...interface{})
- func Debugw(msg string, keysAndValues ...interface{})
- func Error(msg string, fields ...zap.Field)
- func Errorf(template string, args ...interface{})
- func Errorln(args ...interface{})
- func Errorw(msg string, keysAndValues ...interface{})
- func Fatal(msg string, fields ...zap.Field)
- func Fatalf(template string, args ...interface{})
- func Fatalln(args ...interface{})
- func Fatalw(msg string, keysAndValues ...interface{})
- func Info(msg string, fields ...zap.Field)
- func Infof(template string, args ...interface{})
- func Infoln(args ...interface{})
- func Infow(msg string, keysAndValues ...interface{})
- func MultiCore(cores ...zapcore.Core) zapcore.Core
- func MultiWriteSyncer(ws ...zapcore.WriteSyncer) zapcore.WriteSyncer
- func NewCore(enc zapcore.Encoder, ws zapcore.WriteSyncer, levelEnabler zapcore.LevelEnabler, ...) zapcore.Core
- func NewEncoder(options ...EncoderOption) zapcore.Encoder
- func NewIncreaseLevelCore(core zapcore.Core, levelEnabler zapcore.LevelEnabler) (zapcore.Core, error)
- func NewLazyWith(core zapcore.Core, fields []zapcore.Field) zapcore.Core
- func NewLogger(core zapcore.Core, options ...LoggerOption) (logger *zap.Logger)
- func NewNopCore() zapcore.Core
- func NewNopLogger() *zap.Logger
- func NewNopSugaredLogger() *zap.SugaredLogger
- func NewPrefixEncoder(encoder zapcore.Encoder, prefix string) zapcore.Encoder
- func NewSugarLogger(core zapcore.Core, options ...LoggerOption) *zap.SugaredLogger
- func NewWriteSyncer(w io.Writer) zapcore.WriteSyncer
- func NewWriteSyncerWithLock(w io.Writer) zapcore.WriteSyncer
- func Panic(msg string, fields ...zap.Field)
- func Panicf(template string, args ...interface{})
- func Panicln(args ...interface{})
- func Panicw(msg string, keysAndValues ...interface{})
- func PrintBasicDetails(loggers ...*zap.SugaredLogger)
- func ReplaceGlobalLoggers(logger *zap.Logger)
- func StringToLevel(str string) (zapcore.Level, error)
- func Sync()
- func Warn(msg string, fields ...zap.Field)
- func Warnf(template string, args ...interface{})
- func Warnln(args ...interface{})
- func Warnw(msg string, keysAndValues ...interface{})
- type EncoderOption
- func WithEncoderEncodeLevel(encodeLevel zapcore.LevelEncoder) EncoderOption
- func WithEncoderEncodeTime(encodeTime zapcore.TimeEncoder) EncoderOption
- func WithEncoderMessagePrefix(prefix string) EncoderOption
- func WithEncoderOutputFormatConsole() EncoderOption
- func WithEncoderOutputFormatJson() EncoderOption
- type LoggerOption
- func WithAddStacktrace(levelEnabler zapcore.LevelEnabler) LoggerOption
- func WithCaller(caller bool) LoggerOption
- func WithCallerSkip(skip int) LoggerOption
- func WithClock(clock zapcore.Clock) LoggerOption
- func WithDevelopment(flag bool) LoggerOption
- func WithErrorOutput(writeSyncer zapcore.WriteSyncer) LoggerOption
- func WithFields(fields ...zapcore.Field) LoggerOption
- type WrappedLogger
- type WrappedSugaredLogger
Constants ¶
const ( // OutputFormatConsole 人类可读的多行输出 OutputFormatConsole outputFormat = iota // OutputFormatJson JSON格式输出 OutputFormatJson )
Variables ¶
This section is empty.
Functions ¶
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 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 NewLazyWith ¶ added in v3.0.106
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 NewNopSugaredLogger ¶ added in v3.0.106
func NewNopSugaredLogger() *zap.SugaredLogger
NewNopSugaredLogger 不进行实际日志记录操作.
func NewPrefixEncoder ¶ added in v3.0.106
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 PrintBasicDetails ¶ added in v3.0.910
func PrintBasicDetails(loggers ...*zap.SugaredLogger)
func ReplaceGlobalLoggers ¶ added in v3.0.910
ReplaceGlobalLoggers
PS: 有需要的话,应该在应用初始化时调用此方法,即在最前面设置全局logger.
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
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输出了.