简介

zap是有uber开发的一款日志库。

zap提供了三个快速创建Logger方法:

  • NewProduction: 以JSON格式记录Info级别及以上的标准错误日志
  • NewDevelopment: 以JSON格式记录Debug级别及以上的标准错误日志
  • NewExample: 提供一个测试实例,供使用者根据结果进行自定义Logger实例配置
  • New: 实现自定义的Logger

支持的日志级别

zap支持6种日志级别:

const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel = zapcore.DebugLevel
	// InfoLevel is the default logging priority.
	InfoLevel = zapcore.InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel = zapcore.WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel = zapcore.ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel = zapcore.DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel = zapcore.PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel = zapcore.FatalLevel
)

设置Logger日志级别

可以通过WithOption接口为Logger设置日志级别:

log.WithOptions(zap.IncreaseLevel(zapcore.DebugLevel))

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin92
Github: mengbin92
cnblogs: 恋水无意