zapgrpc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. // Package zapgrpc provides a logger that is compatible with grpclog.
  21. package zapgrpc // import "go.uber.org/zap/zapgrpc"
  22. import "go.uber.org/zap"
  23. // An Option overrides a Logger's default configuration.
  24. type Option interface {
  25. apply(*Logger)
  26. }
  27. type optionFunc func(*Logger)
  28. func (f optionFunc) apply(log *Logger) {
  29. f(log)
  30. }
  31. // WithDebug configures a Logger to print at zap's DebugLevel instead of
  32. // InfoLevel.
  33. func WithDebug() Option {
  34. return optionFunc(func(logger *Logger) {
  35. logger.print = (*zap.SugaredLogger).Debug
  36. logger.printf = (*zap.SugaredLogger).Debugf
  37. })
  38. }
  39. // NewLogger returns a new Logger.
  40. //
  41. // By default, Loggers print at zap's InfoLevel.
  42. func NewLogger(l *zap.Logger, options ...Option) *Logger {
  43. logger := &Logger{
  44. log: l.Sugar(),
  45. fatal: (*zap.SugaredLogger).Fatal,
  46. fatalf: (*zap.SugaredLogger).Fatalf,
  47. print: (*zap.SugaredLogger).Info,
  48. printf: (*zap.SugaredLogger).Infof,
  49. }
  50. for _, option := range options {
  51. option.apply(logger)
  52. }
  53. return logger
  54. }
  55. // Logger adapts zap's Logger to be compatible with grpclog.Logger.
  56. type Logger struct {
  57. log *zap.SugaredLogger
  58. fatal func(*zap.SugaredLogger, ...interface{})
  59. fatalf func(*zap.SugaredLogger, string, ...interface{})
  60. print func(*zap.SugaredLogger, ...interface{})
  61. printf func(*zap.SugaredLogger, string, ...interface{})
  62. }
  63. // Fatal implements grpclog.Logger.
  64. func (l *Logger) Fatal(args ...interface{}) {
  65. l.fatal(l.log, args...)
  66. }
  67. // Fatalf implements grpclog.Logger.
  68. func (l *Logger) Fatalf(format string, args ...interface{}) {
  69. l.fatalf(l.log, format, args...)
  70. }
  71. // Fatalln implements grpclog.Logger.
  72. func (l *Logger) Fatalln(args ...interface{}) {
  73. l.fatal(l.log, args...)
  74. }
  75. // Print implements grpclog.Logger.
  76. func (l *Logger) Print(args ...interface{}) {
  77. l.print(l.log, args...)
  78. }
  79. // Printf implements grpclog.Logger.
  80. func (l *Logger) Printf(format string, args ...interface{}) {
  81. l.printf(l.log, format, args...)
  82. }
  83. // Println implements grpclog.Logger.
  84. func (l *Logger) Println(args ...interface{}) {
  85. l.print(l.log, args...)
  86. }