In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the SugaredLogger of zap in golang, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
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 (s.log, s.log, s.log) 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) 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 (s.log, s.log, s.log) 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 provides debug, info, warn, error, panic, dpanic, fatal methods (using the default format of fmt.Sprint) In addition, the method with f supports format, and the method with w supports with key-value pair 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) the level inside zap is divided into debug, info, warn, error, dpanic, panic, fatal, these DPanicDPanic 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 developmentfunc 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 has the same effect as panic under development The final info will not be output DPanic in productionfunc 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 will degenerate to error mode without development, and the final info will still be output, which is safer under 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 method determines lvl If it is zapcore.DPanicLevel, it will further determine whether it is development mode, and if it will set ce.Should (ent, zapcore.WriteThenPanic)
The level inside zap can be divided into debug, info, warn, error, dpanic, panic and fatal.
SugaredLogger provides debug, info, warn, error, panic, dpanic, fatal (using the default format of fmt.Sprint), as well as format with f and with key-value pairs with w.
The effect of DPanic is similar to that of panic in development, but degenerates to error mode in non-development.
The above is all the contents of the article "how to use the SugaredLogger of zap in golang". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.