zap.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "github.com/coreos/etcd/raft"
  17. "go.uber.org/zap"
  18. "go.uber.org/zap/zapcore"
  19. "google.golang.org/grpc/grpclog"
  20. )
  21. // NewGRPCLoggerV2 converts "*zap.Logger" to "grpclog.LoggerV2".
  22. // It discards all INFO level logging in gRPC, if debug level
  23. // is not enabled in "*zap.Logger".
  24. func NewGRPCLoggerV2(lcfg zap.Config) (grpclog.LoggerV2, error) {
  25. lg, err := lcfg.Build(zap.AddCallerSkip(1)) // to annotate caller outside of "logutil"
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &zapGRPCLogger{lg: lg, sugar: lg.Sugar()}, nil
  30. }
  31. type zapGRPCLogger struct {
  32. lg *zap.Logger
  33. sugar *zap.SugaredLogger
  34. }
  35. func (zl *zapGRPCLogger) Info(args ...interface{}) {
  36. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  37. return
  38. }
  39. zl.sugar.Info(args...)
  40. }
  41. func (zl *zapGRPCLogger) Infoln(args ...interface{}) {
  42. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  43. return
  44. }
  45. zl.sugar.Info(args...)
  46. }
  47. func (zl *zapGRPCLogger) Infof(format string, args ...interface{}) {
  48. if !zl.lg.Core().Enabled(zapcore.DebugLevel) {
  49. return
  50. }
  51. zl.sugar.Infof(format, args...)
  52. }
  53. func (zl *zapGRPCLogger) Warning(args ...interface{}) {
  54. zl.sugar.Warn(args...)
  55. }
  56. func (zl *zapGRPCLogger) Warningln(args ...interface{}) {
  57. zl.sugar.Warn(args...)
  58. }
  59. func (zl *zapGRPCLogger) Warningf(format string, args ...interface{}) {
  60. zl.sugar.Warnf(format, args...)
  61. }
  62. func (zl *zapGRPCLogger) Error(args ...interface{}) {
  63. zl.sugar.Error(args...)
  64. }
  65. func (zl *zapGRPCLogger) Errorln(args ...interface{}) {
  66. zl.sugar.Error(args...)
  67. }
  68. func (zl *zapGRPCLogger) Errorf(format string, args ...interface{}) {
  69. zl.sugar.Errorf(format, args...)
  70. }
  71. func (zl *zapGRPCLogger) Fatal(args ...interface{}) {
  72. zl.sugar.Fatal(args...)
  73. }
  74. func (zl *zapGRPCLogger) Fatalln(args ...interface{}) {
  75. zl.sugar.Fatal(args...)
  76. }
  77. func (zl *zapGRPCLogger) Fatalf(format string, args ...interface{}) {
  78. zl.sugar.Fatalf(format, args...)
  79. }
  80. func (zl *zapGRPCLogger) V(l int) bool {
  81. // infoLog == 0
  82. if l <= 0 { // debug level, then we ignore info level in gRPC
  83. return !zl.lg.Core().Enabled(zapcore.DebugLevel)
  84. }
  85. return true
  86. }
  87. // NewRaftLogger converts "*zap.Logger" to "raft.Logger".
  88. func NewRaftLogger(lcfg zap.Config) (raft.Logger, error) {
  89. lg, err := lcfg.Build(zap.AddCallerSkip(1)) // to annotate caller outside of "logutil"
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &zapRaftLogger{lg: lg, sugar: lg.Sugar()}, nil
  94. }
  95. type zapRaftLogger struct {
  96. lg *zap.Logger
  97. sugar *zap.SugaredLogger
  98. }
  99. func (zl *zapRaftLogger) Debug(args ...interface{}) {
  100. zl.sugar.Debug(args...)
  101. }
  102. func (zl *zapRaftLogger) Debugf(format string, args ...interface{}) {
  103. zl.sugar.Debugf(format, args...)
  104. }
  105. func (zl *zapRaftLogger) Error(args ...interface{}) {
  106. zl.sugar.Error(args...)
  107. }
  108. func (zl *zapRaftLogger) Errorf(format string, args ...interface{}) {
  109. zl.sugar.Errorf(format, args...)
  110. }
  111. func (zl *zapRaftLogger) Info(args ...interface{}) {
  112. zl.sugar.Info(args...)
  113. }
  114. func (zl *zapRaftLogger) Infof(format string, args ...interface{}) {
  115. zl.sugar.Infof(format, args...)
  116. }
  117. func (zl *zapRaftLogger) Warning(args ...interface{}) {
  118. zl.sugar.Warn(args...)
  119. }
  120. func (zl *zapRaftLogger) Warningf(format string, args ...interface{}) {
  121. zl.sugar.Warnf(format, args...)
  122. }
  123. func (zl *zapRaftLogger) Fatal(args ...interface{}) {
  124. zl.sugar.Fatal(args...)
  125. }
  126. func (zl *zapRaftLogger) Fatalf(format string, args ...interface{}) {
  127. zl.sugar.Fatalf(format, args...)
  128. }
  129. func (zl *zapRaftLogger) Panic(args ...interface{}) {
  130. zl.sugar.Panic(args...)
  131. }
  132. func (zl *zapRaftLogger) Panicf(format string, args ...interface{}) {
  133. zl.sugar.Panicf(format, args...)
  134. }