golang中zap的SugaredLogger怎么用
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,小编给大家分享一下golang中zap的SugaredLogger怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Su
千家信息网最后更新 2026年11月01日golang中zap的SugaredLogger怎么用
小编给大家分享一下golang中zap的SugaredLogger怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
SugaredLogger
zap@v1.16.0/sugar.go
type SugaredLogger struct { base *Logger}func (s *SugaredLogger) Named(name string) *SugaredLogger { return &SugaredLogger{base: s.base.Named(name)}}func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger { return &SugaredLogger{base: s.base.With(s.sweetenFields(args)...)}}func (s *SugaredLogger) Debug(args ...interface{}) { s.log(DebugLevel, "", args, nil)}func (s *SugaredLogger) Info(args ...interface{}) { s.log(InfoLevel, "", args, nil)}func (s *SugaredLogger) Warn(args ...interface{}) { s.log(WarnLevel, "", args, nil)}func (s *SugaredLogger) Error(args ...interface{}) { s.log(ErrorLevel, "", args, nil)}func (s *SugaredLogger) DPanic(args ...interface{}) { s.log(DPanicLevel, "", args, nil)}func (s *SugaredLogger) Panic(args ...interface{}) { s.log(PanicLevel, "", args, nil)}func (s *SugaredLogger) Fatal(args ...interface{}) { s.log(FatalLevel, "", args, nil)}func (s *SugaredLogger) Debugf(template string, args ...interface{}) { s.log(DebugLevel, template, args, nil)}func (s *SugaredLogger) Infof(template string, args ...interface{}) { s.log(InfoLevel, template, args, nil)}func (s *SugaredLogger) Warnf(template string, args ...interface{}) { s.log(WarnLevel, template, args, nil)}func (s *SugaredLogger) Errorf(template string, args ...interface{}) { s.log(ErrorLevel, template, args, nil)}func (s *SugaredLogger) DPanicf(template string, args ...interface{}) { s.log(DPanicLevel, template, args, nil)}func (s *SugaredLogger) Panicf(template string, args ...interface{}) { s.log(PanicLevel, template, args, nil)}func (s *SugaredLogger) Fatalf(template string, args ...interface{}) { s.log(FatalLevel, template, args, nil)}func (s *SugaredLogger) Debugw(msg string, keysAndValues ...interface{}) { s.log(DebugLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) Infow(msg string, keysAndValues ...interface{}) { s.log(InfoLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) Warnw(msg string, keysAndValues ...interface{}) { s.log(WarnLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) Errorw(msg string, keysAndValues ...interface{}) { s.log(ErrorLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) DPanicw(msg string, keysAndValues ...interface{}) { s.log(DPanicLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) Panicw(msg string, keysAndValues ...interface{}) { s.log(PanicLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) Fatalw(msg string, keysAndValues ...interface{}) { s.log(FatalLevel, msg, nil, keysAndValues)}func (s *SugaredLogger) Sync() error { return s.base.Sync()}SugaredLogger提供了debug、info、warn、error、panic、dpanic、fatal这几种方法(使用fmt.Sprint的默认格式),另外还有带f的支持format,带w的方法则支持with键值对level
zap@v1.16.0/level.go
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 inpidual // 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)
zap内部的level分为debug、info、warn、error、dpanic、panic、fatal这几种
DPanic
DPanic stands for "panic in development." In development, it logs at PanicLevel; otherwise, it logs at ErrorLevel. DPanic makes it easier to catch errors that are theoretically possible, but shouldn't actually happen, without crashing in production.
DPanic in development
func dpanicInDevelopment() { logger, _ := zap.NewDevelopment() defer logger.Sync() // flushes buffer, if any sugar := logger.Sugar() sugar.DPanic("test dpanic") sugar.Info("this will not be logged")}DPanic在development下的效果跟panic效果类似,最后的info不会被输出
DPanic in production
func dpanicInProduction() { logger, _ := zap.NewProduction() defer logger.Sync() // flushes buffer, if any sugar := logger.Sugar() sugar.DPanic("test dpanic logged as error in not development mode") sugar.Info("this will be logged")}DPanic在非development下则退化为error模式,最后的info照样会输出,这样子在production下比较安全一点。
logger.check
zap@v1.16.0/logger.go
func (log *Logger) check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry { // check must always be called directly by a method in the Logger interface // (e.g., Check, Info, Fatal). const callerSkipOffset = 2 // Check the level first to reduce the cost of disabled log calls. // Since Panic and higher may exit, we skip the optimization for those levels. if lvl < zapcore.DPanicLevel && !log.core.Enabled(lvl) { return nil } // Create basic checked entry thru the core; this will be non-nil if the // log message will actually be written somewhere. ent := zapcore.Entry{ LoggerName: log.name, Time: time.Now(), Level: lvl, Message: msg, } ce := log.core.Check(ent, nil) willWrite := ce != nil // Set up any required terminal behavior. switch ent.Level { case zapcore.PanicLevel: ce = ce.Should(ent, zapcore.WriteThenPanic) case zapcore.FatalLevel: onFatal := log.onFatal // Noop is the default value for CheckWriteAction, and it leads to // continued execution after a Fatal which is unexpected. if onFatal == zapcore.WriteThenNoop { onFatal = zapcore.WriteThenFatal } ce = ce.Should(ent, onFatal) case zapcore.DPanicLevel: if log.development { ce = ce.Should(ent, zapcore.WriteThenPanic) } } // Only do further annotation if we're going to write this message; checked // entries that exist only for terminal behavior don't benefit from // annotation. if !willWrite { return ce } // Thread the error output through to the CheckedEntry. ce.ErrorOutput = log.errorOutput if log.addCaller { frame, defined := getCallerFrame(log.callerSkip + callerSkipOffset) if !defined { fmt.Fprintf(log.errorOutput, "%v Logger.check error: failed to get caller\n", time.Now().UTC()) log.errorOutput.Sync() } ce.Entry.Caller = zapcore.EntryCaller{ Defined: defined, PC: frame.PC, File: frame.File, Line: frame.Line, Function: frame.Function, } } if log.addStack.Enabled(ce.Entry.Level) { ce.Entry.Stack = StackSkip("", log.callerSkip+callerSkipOffset).String } return ce}logger.check方法会判断lvl,如果是zapcore.DPanicLevel,则会进一步判断是否是development模式,如果是会设置ce.Should(ent, zapcore.WriteThenPanic)zap内部的level分为debug、info、warn、error、dpanic、panic、fatal这几种
SugaredLogger提供了debug、info、warn、error、panic、dpanic、fatal这几种方法(
使用fmt.Sprint的默认格式),另外还有带f的支持format,带w的方法则支持with键值对DPanic在development下的效果跟panic效果类似,在非development下则退化为error模式
以上是"golang中zap的SugaredLogger怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
方法
效果
支持
模式
篇文章
内容
格式
输出
安全
不怎么
大部分
更多
样子
知识
行业
资讯
资讯频道
频道
进一
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
博彦科技互联网金融
广州万易互联网科技有限公司
国智互联网科技
互联网巨头反垄断金融科技
科技类互联网
易互联网科技有限公司
互联网金融公司科技
万易互联网科技有限公司
九亚互联网科技
极限挑战互联网科技
广州凡科互联网科技
互联网 视角下的科技创新
互联网科技宣传片
互联网科技热点资讯
健康食品互联网创新科技园项目
深圳艾果互联网通讯科技
用友工业互联网科技发展
互联网金融科技名词解释
深圳互联网科技园地址
六子台科技互联网展会
互联网科技工作委员会
易互联网科技有限公司
狮子座互联网科技有限公司
天津摩尔互联网科技
汇久创客互联网科技
河南猎农互联网科技有限公司
互联网金融科技名词解释
互联网科技资金流入
厦门江牛科技互联网
绵阳真晨互联网科技有限公司