Documentation ¶
Index ¶
- Constants
- Variables
- 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 Exit(codes ...int)
- 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 L() *zap.Logger
- func MultiCore(cores ...zapcore.Core) zapcore.Core
- 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 NewStdLogger(l *zap.Logger) *log.Logger
- func NewStdLoggerWithLevel(l *zap.Logger, level zapcore.Level) (*log.Logger, error)
- func NewSugarLogger(core zapcore.Core, options ...LoggerOption) *zap.SugaredLogger
- func Panic(msg string, fields ...zap.Field)
- func Panicf(template string, args ...interface{})
- func Panicln(args ...interface{})
- func Panicw(msg string, keysAndValues ...interface{})
- func RedirectStdLog(l *zap.Logger) func()
- func RedirectStdLogWithLevel(l *zap.Logger, level zapcore.Level) (func(), error)
- func RegisterExitHandler(handlers ...func())
- func RegisterParallelExitHandler(handlers ...func())
- func RunExitHandlers()
- func S() *zap.SugaredLogger
- func SetExitTimeout(d time.Duration)
- 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 WithFatalHook(hook zapcore.CheckWriteHook) LoggerOptiondeprecated
- func WithFields(fields ...zapcore.Field) LoggerOption
- func WithPanicHook(hook zapcore.CheckWriteHook) LoggerOption
- type WrappedLogger
- type WrappedSugaredLogger
Constants ¶
const ( // OutputFormatConsole 人类可读的多行输出 OutputFormatConsole outputFormat = iota // OutputFormatJson JSON格式输出 OutputFormatJson )
Variables ¶
var ( LockedWriteSyncerStdout = ioKit.LockedWriteSyncerStdout LockedWriteSyncerStderr = ioKit.LockedWriteSyncerStderr )
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 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 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 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) [Core] 仅有1个输出: 输出到控制台(并发安全地输出到os.Stdout) (6) [Core] 仅有1个输出: 日志级别为 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 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 NewStdLogger ¶ added in v3.0.106
NewStdLogger *zap.Logger => *log.Logger
func NewStdLoggerWithLevel ¶ added in v3.0.106
NewStdLoggerWithLevel *zap.Logger => *log.Logger
func NewSugarLogger ¶ added in v3.0.106
func NewSugarLogger(core zapcore.Core, options ...LoggerOption) *zap.SugaredLogger
func RedirectStdLog ¶ added in v3.0.106
RedirectStdLog 重定向 log标准库 的输出
func RedirectStdLogWithLevel ¶ added in v3.0.106
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 SetExitTimeout ¶ added in v3.0.106
SetExitTimeout 执行所有exit handler的超时时间.
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
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输出了.