rpclogger_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package internal
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. )
  8. const content = "foo"
  9. func TestLoggerError(t *testing.T) {
  10. w, restore := injectLog()
  11. defer restore()
  12. logger := new(Logger)
  13. logger.Error(content)
  14. assert.Contains(t, w.String(), content)
  15. }
  16. func TestLoggerErrorf(t *testing.T) {
  17. w, restore := injectLog()
  18. defer restore()
  19. logger := new(Logger)
  20. logger.Errorf(content)
  21. assert.Contains(t, w.String(), content)
  22. }
  23. func TestLoggerErrorln(t *testing.T) {
  24. w, restore := injectLog()
  25. defer restore()
  26. logger := new(Logger)
  27. logger.Errorln(content)
  28. assert.Contains(t, w.String(), content)
  29. }
  30. func TestLoggerFatal(t *testing.T) {
  31. w, restore := injectLog()
  32. defer restore()
  33. logger := new(Logger)
  34. logger.Fatal(content)
  35. assert.Contains(t, w.String(), content)
  36. }
  37. func TestLoggerFatalf(t *testing.T) {
  38. w, restore := injectLog()
  39. defer restore()
  40. logger := new(Logger)
  41. logger.Fatalf(content)
  42. assert.Contains(t, w.String(), content)
  43. }
  44. func TestLoggerFatalln(t *testing.T) {
  45. w, restore := injectLog()
  46. defer restore()
  47. logger := new(Logger)
  48. logger.Fatalln(content)
  49. assert.Contains(t, w.String(), content)
  50. }
  51. func TestLoggerInfo(t *testing.T) {
  52. w, restore := injectLog()
  53. defer restore()
  54. logger := new(Logger)
  55. logger.Info(content)
  56. assert.Empty(t, w.String())
  57. }
  58. func TestLoggerInfof(t *testing.T) {
  59. w, restore := injectLog()
  60. defer restore()
  61. logger := new(Logger)
  62. logger.Infof(content)
  63. assert.Empty(t, w.String())
  64. }
  65. func TestLoggerWarning(t *testing.T) {
  66. w, restore := injectLog()
  67. defer restore()
  68. logger := new(Logger)
  69. logger.Warning(content)
  70. assert.Empty(t, w.String())
  71. }
  72. func TestLoggerInfoln(t *testing.T) {
  73. w, restore := injectLog()
  74. defer restore()
  75. logger := new(Logger)
  76. logger.Infoln(content)
  77. assert.Empty(t, w.String())
  78. }
  79. func TestLoggerWarningf(t *testing.T) {
  80. w, restore := injectLog()
  81. defer restore()
  82. logger := new(Logger)
  83. logger.Warningf(content)
  84. assert.Empty(t, w.String())
  85. }
  86. func TestLoggerWarningln(t *testing.T) {
  87. w, restore := injectLog()
  88. defer restore()
  89. logger := new(Logger)
  90. logger.Warningln(content)
  91. assert.Empty(t, w.String())
  92. }
  93. func TestLogger_V(t *testing.T) {
  94. logger := new(Logger)
  95. // grpclog.fatalLog
  96. assert.True(t, logger.V(3))
  97. // grpclog.infoLog
  98. assert.False(t, logger.V(0))
  99. }
  100. func injectLog() (r *strings.Builder, restore func()) {
  101. var buf strings.Builder
  102. w := logx.NewWriter(&buf)
  103. o := logx.Reset()
  104. logx.SetWriter(w)
  105. return &buf, func() {
  106. logx.Reset()
  107. logx.SetWriter(o)
  108. }
  109. }