blob: f016b3054e3570125a099f9ca46c36b0c3261235 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -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
Scott Baker8487c5d2019-10-18 12:49:46 -070026// NewRaftLogger builds "raft.Logger" from "*zap.Config".
Scott Baker2d897982019-09-24 11:50:08 -070027func 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
Scott Baker8487c5d2019-10-18 12:49:46 -070038// NewRaftLoggerZap converts "*zap.Logger" to "raft.Logger".
39func NewRaftLoggerZap(lg *zap.Logger) raft.Logger {
40 return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
41}
42
Scott Baker2d897982019-09-24 11:50:08 -070043// NewRaftLoggerFromZapCore creates "raft.Logger" from "zap.Core"
44// and "zapcore.WriteSyncer".
45func NewRaftLoggerFromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) raft.Logger {
46 // "AddCallerSkip" to annotate caller outside of "logutil"
47 lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
48 return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
49}
50
51type zapRaftLogger struct {
52 lg *zap.Logger
53 sugar *zap.SugaredLogger
54}
55
56func (zl *zapRaftLogger) Debug(args ...interface{}) {
57 zl.sugar.Debug(args...)
58}
59
60func (zl *zapRaftLogger) Debugf(format string, args ...interface{}) {
61 zl.sugar.Debugf(format, args...)
62}
63
64func (zl *zapRaftLogger) Error(args ...interface{}) {
65 zl.sugar.Error(args...)
66}
67
68func (zl *zapRaftLogger) Errorf(format string, args ...interface{}) {
69 zl.sugar.Errorf(format, args...)
70}
71
72func (zl *zapRaftLogger) Info(args ...interface{}) {
73 zl.sugar.Info(args...)
74}
75
76func (zl *zapRaftLogger) Infof(format string, args ...interface{}) {
77 zl.sugar.Infof(format, args...)
78}
79
80func (zl *zapRaftLogger) Warning(args ...interface{}) {
81 zl.sugar.Warn(args...)
82}
83
84func (zl *zapRaftLogger) Warningf(format string, args ...interface{}) {
85 zl.sugar.Warnf(format, args...)
86}
87
88func (zl *zapRaftLogger) Fatal(args ...interface{}) {
89 zl.sugar.Fatal(args...)
90}
91
92func (zl *zapRaftLogger) Fatalf(format string, args ...interface{}) {
93 zl.sugar.Fatalf(format, args...)
94}
95
96func (zl *zapRaftLogger) Panic(args ...interface{}) {
97 zl.sugar.Panic(args...)
98}
99
100func (zl *zapRaftLogger) Panicf(format string, args ...interface{}) {
101 zl.sugar.Panicf(format, args...)
102}