rpclogger.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package internal
  2. import (
  3. "sync"
  4. "github.com/tal-tech/go-zero/core/logx"
  5. "google.golang.org/grpc/grpclog"
  6. )
  7. // because grpclog.errorLog is not exported, we need to define our own.
  8. const errorLevel = 2
  9. var once sync.Once
  10. // A Logger is a rpc logger.
  11. type Logger struct{}
  12. // InitLogger initializes the rpc logger.
  13. func InitLogger() {
  14. once.Do(func() {
  15. grpclog.SetLoggerV2(new(Logger))
  16. })
  17. }
  18. // Error logs the given args into error log.
  19. func (l *Logger) Error(args ...interface{}) {
  20. logx.Error(args...)
  21. }
  22. // Errorf logs the given args with format into error log.
  23. func (l *Logger) Errorf(format string, args ...interface{}) {
  24. logx.Errorf(format, args...)
  25. }
  26. // Errorln logs the given args into error log with newline.
  27. func (l *Logger) Errorln(args ...interface{}) {
  28. logx.Error(args...)
  29. }
  30. // Fatal logs the given args into error log.
  31. func (l *Logger) Fatal(args ...interface{}) {
  32. logx.Error(args...)
  33. }
  34. // Fatalf logs the given args with format into error log.
  35. func (l *Logger) Fatalf(format string, args ...interface{}) {
  36. logx.Errorf(format, args...)
  37. }
  38. // Fatalln logs args into error log with newline.
  39. func (l *Logger) Fatalln(args ...interface{}) {
  40. logx.Error(args...)
  41. }
  42. // Info ignores the grpc info logs.
  43. func (l *Logger) Info(args ...interface{}) {
  44. // ignore builtin grpc info
  45. }
  46. // Infoln ignores the grpc info logs.
  47. func (l *Logger) Infoln(args ...interface{}) {
  48. // ignore builtin grpc info
  49. }
  50. // Infof ignores the grpc info logs.
  51. func (l *Logger) Infof(format string, args ...interface{}) {
  52. // ignore builtin grpc info
  53. }
  54. // V checks if meet required log level.
  55. func (l *Logger) V(v int) bool {
  56. return v >= errorLevel
  57. }
  58. // Warning ignores the grpc warning logs.
  59. func (l *Logger) Warning(args ...interface{}) {
  60. // ignore builtin grpc warning
  61. }
  62. // Warningf ignores the grpc warning logs.
  63. func (l *Logger) Warningf(format string, args ...interface{}) {
  64. // ignore builtin grpc warning
  65. }
  66. // Warningln ignores the grpc warning logs.
  67. func (l *Logger) Warningln(args ...interface{}) {
  68. // ignore builtin grpc warning
  69. }