zap自定义日志级别
简介
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: 恋水无意
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步