William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | package logutil |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | |
| 20 | "go.etcd.io/etcd/raft" |
| 21 | |
| 22 | "go.uber.org/zap" |
| 23 | "go.uber.org/zap/zapcore" |
| 24 | ) |
| 25 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 26 | // NewRaftLogger builds "raft.Logger" from "*zap.Config". |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 27 | func 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 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 38 | // NewRaftLoggerZap converts "*zap.Logger" to "raft.Logger". |
| 39 | func NewRaftLoggerZap(lg *zap.Logger) raft.Logger { |
| 40 | return &zapRaftLogger{lg: lg, sugar: lg.Sugar()} |
| 41 | } |
| 42 | |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 43 | // NewRaftLoggerFromZapCore creates "raft.Logger" from "zap.Core" |
| 44 | // and "zapcore.WriteSyncer". |
| 45 | func 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 | |
| 51 | type zapRaftLogger struct { |
| 52 | lg *zap.Logger |
| 53 | sugar *zap.SugaredLogger |
| 54 | } |
| 55 | |
| 56 | func (zl *zapRaftLogger) Debug(args ...interface{}) { |
| 57 | zl.sugar.Debug(args...) |
| 58 | } |
| 59 | |
| 60 | func (zl *zapRaftLogger) Debugf(format string, args ...interface{}) { |
| 61 | zl.sugar.Debugf(format, args...) |
| 62 | } |
| 63 | |
| 64 | func (zl *zapRaftLogger) Error(args ...interface{}) { |
| 65 | zl.sugar.Error(args...) |
| 66 | } |
| 67 | |
| 68 | func (zl *zapRaftLogger) Errorf(format string, args ...interface{}) { |
| 69 | zl.sugar.Errorf(format, args...) |
| 70 | } |
| 71 | |
| 72 | func (zl *zapRaftLogger) Info(args ...interface{}) { |
| 73 | zl.sugar.Info(args...) |
| 74 | } |
| 75 | |
| 76 | func (zl *zapRaftLogger) Infof(format string, args ...interface{}) { |
| 77 | zl.sugar.Infof(format, args...) |
| 78 | } |
| 79 | |
| 80 | func (zl *zapRaftLogger) Warning(args ...interface{}) { |
| 81 | zl.sugar.Warn(args...) |
| 82 | } |
| 83 | |
| 84 | func (zl *zapRaftLogger) Warningf(format string, args ...interface{}) { |
| 85 | zl.sugar.Warnf(format, args...) |
| 86 | } |
| 87 | |
| 88 | func (zl *zapRaftLogger) Fatal(args ...interface{}) { |
| 89 | zl.sugar.Fatal(args...) |
| 90 | } |
| 91 | |
| 92 | func (zl *zapRaftLogger) Fatalf(format string, args ...interface{}) { |
| 93 | zl.sugar.Fatalf(format, args...) |
| 94 | } |
| 95 | |
| 96 | func (zl *zapRaftLogger) Panic(args ...interface{}) { |
| 97 | zl.sugar.Panic(args...) |
| 98 | } |
| 99 | |
| 100 | func (zl *zapRaftLogger) Panicf(format string, args ...interface{}) { |
| 101 | zl.sugar.Panicf(format, args...) |
| 102 | } |