您好,欢迎访问一九零五行业门户网

带你研究一下go zap的SugaredLogger!

本文由go语言教程栏目给大家介绍golang的zap的sugaredlogger,希望对需要的朋友有所帮助!序本文主要研究一下golang的zap的sugaredlogger
sugaredloggerzap@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键值对levelzap@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这几种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在development下的效果跟panic效果类似,最后的info不会被输出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在非development下则退化为error模式,最后的info照样会输出,这样子在production下比较安全一点。logger.checkzap@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模式以上就是带你研究一下go zap的sugaredlogger!的详细内容。
其它类似信息

推荐信息