zap_raft.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "github.com/coreos/etcd/raft"
  18. "go.uber.org/zap"
  19. "go.uber.org/zap/zapcore"
  20. )
  21. // NewRaftLogger converts "*zap.Logger" to "raft.Logger".
  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. // NewRaftLoggerFromZapCore creates "raft.Logger" from "zap.Core"
  33. // and "zapcore.WriteSyncer".
  34. func NewRaftLoggerFromZapCore(cr zapcore.Core, syncer zapcore.WriteSyncer) raft.Logger {
  35. // "AddCallerSkip" to annotate caller outside of "logutil"
  36. lg := zap.New(cr, zap.AddCaller(), zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
  37. return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}
  38. }
  39. type zapRaftLogger struct {
  40. lg *zap.Logger
  41. sugar *zap.SugaredLogger
  42. }
  43. func (zl *zapRaftLogger) Debug(args ...interface{}) {
  44. zl.sugar.Debug(args...)
  45. }
  46. func (zl *zapRaftLogger) Debugf(format string, args ...interface{}) {
  47. zl.sugar.Debugf(format, args...)
  48. }
  49. func (zl *zapRaftLogger) Error(args ...interface{}) {
  50. zl.sugar.Error(args...)
  51. }
  52. func (zl *zapRaftLogger) Errorf(format string, args ...interface{}) {
  53. zl.sugar.Errorf(format, args...)
  54. }
  55. func (zl *zapRaftLogger) Info(args ...interface{}) {
  56. zl.sugar.Info(args...)
  57. }
  58. func (zl *zapRaftLogger) Infof(format string, args ...interface{}) {
  59. zl.sugar.Infof(format, args...)
  60. }
  61. func (zl *zapRaftLogger) Warning(args ...interface{}) {
  62. zl.sugar.Warn(args...)
  63. }
  64. func (zl *zapRaftLogger) Warningf(format string, args ...interface{}) {
  65. zl.sugar.Warnf(format, args...)
  66. }
  67. func (zl *zapRaftLogger) Fatal(args ...interface{}) {
  68. zl.sugar.Fatal(args...)
  69. }
  70. func (zl *zapRaftLogger) Fatalf(format string, args ...interface{}) {
  71. zl.sugar.Fatalf(format, args...)
  72. }
  73. func (zl *zapRaftLogger) Panic(args ...interface{}) {
  74. zl.sugar.Panic(args...)
  75. }
  76. func (zl *zapRaftLogger) Panicf(format string, args ...interface{}) {
  77. zl.sugar.Panicf(format, args...)
  78. }