blob: e92cba04cbf46f51d72e8bd2d830218116eeeb19 [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -07001// Copyright 2018 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package logutil
16
17import (
18 "errors"
19
20 "go.etcd.io/etcd/raft"
21
22 "go.uber.org/zap"
23 "go.uber.org/zap/zapcore"
24)
25
26// NewRaftLogger converts "*zap.Logger" to "raft.Logger".
27func NewRaftLogger(lcfg *zap.Config) (raft.Logger, error) {
28 if lcfg == nil {
29 return nil, errors.New("nil zap.Config")
30 }
31 lg, err := lcfg.Build(zap.AddCallerSkip(1)) // to annotate caller outside of "logutil"
32 if err != nil {
33 return nil, err
34 }
35 return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}, nil
36}
37
38// NewRaftLoggerFromZapCore creates "raft.Logger" from "zap.Core"
39// and "zapcore.WriteSyncer".
40func NewRaftLoggerFromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) raft.Logger {
41 // "AddCallerSkip" to annotate caller outside of "logutil"
42 lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
43 return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
44}
45
46type zapRaftLogger struct {
47 lg *zap.Logger
48 sugar *zap.SugaredLogger
49}
50
51func (zl *zapRaftLogger) Debug(args ...interface{}) {
52 zl.sugar.Debug(args...)
53}
54
55func (zl *zapRaftLogger) Debugf(format string, args ...interface{}) {
56 zl.sugar.Debugf(format, args...)
57}
58
59func (zl *zapRaftLogger) Error(args ...interface{}) {
60 zl.sugar.Error(args...)
61}
62
63func (zl *zapRaftLogger) Errorf(format string, args ...interface{}) {
64 zl.sugar.Errorf(format, args...)
65}
66
67func (zl *zapRaftLogger) Info(args ...interface{}) {
68 zl.sugar.Info(args...)
69}
70
71func (zl *zapRaftLogger) Infof(format string, args ...interface{}) {
72 zl.sugar.Infof(format, args...)
73}
74
75func (zl *zapRaftLogger) Warning(args ...interface{}) {
76 zl.sugar.Warn(args...)
77}
78
79func (zl *zapRaftLogger) Warningf(format string, args ...interface{}) {
80 zl.sugar.Warnf(format, args...)
81}
82
83func (zl *zapRaftLogger) Fatal(args ...interface{}) {
84 zl.sugar.Fatal(args...)
85}
86
87func (zl *zapRaftLogger) Fatalf(format string, args ...interface{}) {
88 zl.sugar.Fatalf(format, args...)
89}
90
91func (zl *zapRaftLogger) Panic(args ...interface{}) {
92 zl.sugar.Panic(args...)
93}
94
95func (zl *zapRaftLogger) Panicf(format string, args ...interface{}) {
96 zl.sugar.Panicf(format, args...)
97}