golang 判断时间是否是 rfc3339_golang的zap的SugaredLogger

本文主要研究一下golang的zap的SugaredLogger

5efa098d82e8b68a24351e8a9c7af332.png

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 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)

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 

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模式

doc

  • zap
  • What's DPanic?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值