zap_raft.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. package logutil
  15. import (
  16. "errors"
  17. "go.etcd.io/etcd/raft"
  18. "go.uber.org/zap"
  19. "go.uber.org/zap/zapcore"
  20. )
  21. // NewRaftLogger builds "raft.Logger" from "*zap.Config".
  22. func NewRaftLogger(lcfg *zap.Config) (raft.Logger, error) {
  23. if lcfg == nil {
  24. return nil, errors.New("nil zap.Config")
  25. }
  26. lg, err := lcfg.Build(zap.AddCallerSkip(1)) // to annotate caller outside of "logutil"
  27. if err != nil {
  28. return nil, err
  29. }
  30. return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}, nil
  31. }
  32. // NewRaftLoggerZap converts "*zap.Logger" to "raft.Logger".
  33. func NewRaftLoggerZap(lg *zap.Logger) raft.Logger {
  34. return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
  35. }
  36. // NewRaftLoggerFromZapCore creates "raft.Logger" from "zap.Core"
  37. // and "zapcore.WriteSyncer".
  38. func NewRaftLoggerFromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) raft.Logger {
  39. // "AddCallerSkip" to annotate caller outside of "logutil"
  40. lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
  41. return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
  42. }
  43. type zapRaftLogger struct {
  44. lg *zap.Logger
  45. sugar *zap.SugaredLogger
  46. }
  47. func (zl *zapRaftLogger) Debug(args ...interface{}) {
  48. zl.sugar.Debug(args...)
  49. }
  50. func (zl *zapRaftLogger) Debugf(format string, args ...interface{}) {
  51. zl.sugar.Debugf(format, args...)
  52. }
  53. func (zl *zapRaftLogger) Error(args ...interface{}) {
  54. zl.sugar.Error(args...)
  55. }
  56. func (zl *zapRaftLogger) Errorf(format string, args ...interface{}) {
  57. zl.sugar.Errorf(format, args...)
  58. }
  59. func (zl *zapRaftLogger) Info(args ...interface{}) {
  60. zl.sugar.Info(args...)
  61. }
  62. func (zl *zapRaftLogger) Infof(format string, args ...interface{}) {
  63. zl.sugar.Infof(format, args...)
  64. }
  65. func (zl *zapRaftLogger) Warning(args ...interface{}) {
  66. zl.sugar.Warn(args...)
  67. }
  68. func (zl *zapRaftLogger) Warningf(format string, args ...interface{}) {
  69. zl.sugar.Warnf(format, args...)
  70. }
  71. func (zl *zapRaftLogger) Fatal(args ...interface{}) {
  72. zl.sugar.Fatal(args...)
  73. }
  74. func (zl *zapRaftLogger) Fatalf(format string, args ...interface{}) {
  75. zl.sugar.Fatalf(format, args...)
  76. }
  77. func (zl *zapRaftLogger) Panic(args ...interface{}) {
  78. zl.sugar.Panic(args...)
  79. }
  80. func (zl *zapRaftLogger) Panicf(format string, args ...interface{}) {
  81. zl.sugar.Panicf(format, args...)
  82. }