ホームページ  >  記事  >  バックエンド開発  >  go zap の SugaredLogger について勉強させてください。

go zap の SugaredLogger について勉強させてください。

藏色散人
藏色散人転載
2021-09-13 15:41:242112ブラウズ

まえがき

この記事では主に 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 は、デバッグ、情報、警告、エラー、パニック、dpanic、致命的なメソッド (fmt.Sprint のデフォルト形式を使用) を提供し、f を使用した形式もサポートします。w メソッドは、キーと値のペア

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 内のレベルは、debug、info、warn、error、dpanic に分かれています、パニック、致命的

DPanic

DPanic は、「開発中のパニック」の略です。開発中は、PanicLevel でログに記録されます。それ以外の場合は、ErrorLevel でログに記録されます。DPanic を使用すると、次のようなエラーを簡単にキャッチできます。理論的には可能ですが、運用環境でクラッシュすることなく、実際に起こるべきではありません。

開発中の DPanic

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 の影響は、パニックの影響と似ています。最終的な情報は表示されません。本番環境での出力

DPanic

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 は、非開発条件下ではエラー モードに縮退しますが、最終情報は引き続き出力されるため、本番環境ではより安全です。

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 メソッドはレベルを決定します。zapcore.DPanicLevel の場合は、開発モードであるかどうかをさらに決定します。開発モードである場合は、ce.Should(ent, zapcore.WriteThenPanic)

summary

  • zap external が設定されます。レベルは、debug、info、warn、error、dpanic、panic、fatal に分かれています。
  • SugaredLogger は、debug、info、warn、error、panic、dpanic、fatal メソッドを提供します (fmt のデフォルト形式を使用します。 Sprint)、さらに、f を使用したメソッドはフォーマットをサポートし、w を使用したメソッドはキーと値のペアをサポートします。
  • 開発中の DPanic の効果はパニック効果に似ており、次のように縮退します。非開発中のエラーモード

以上がgo zap の SugaredLogger について勉強させてください。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はsegmentfault.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。