1
0

logs_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package logc
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/wuntsong-org/go-zero-plus/core/logx"
  11. "github.com/wuntsong-org/go-zero-plus/core/logx/logtest"
  12. )
  13. func TestAddGlobalFields(t *testing.T) {
  14. buf := logtest.NewCollector(t)
  15. Info(context.Background(), "hello")
  16. buf.Reset()
  17. AddGlobalFields(Field("a", "1"), Field("b", "2"))
  18. AddGlobalFields(Field("c", "3"))
  19. Info(context.Background(), "world")
  20. var m map[string]any
  21. assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
  22. assert.Equal(t, "1", m["a"])
  23. assert.Equal(t, "2", m["b"])
  24. assert.Equal(t, "3", m["c"])
  25. }
  26. func TestAlert(t *testing.T) {
  27. buf := logtest.NewCollector(t)
  28. Alert(context.Background(), "foo")
  29. assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
  30. }
  31. func TestError(t *testing.T) {
  32. buf := logtest.NewCollector(t)
  33. file, line := getFileLine()
  34. Error(context.Background(), "foo")
  35. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  36. }
  37. func TestErrorf(t *testing.T) {
  38. buf := logtest.NewCollector(t)
  39. file, line := getFileLine()
  40. Errorf(context.Background(), "foo %s", "bar")
  41. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  42. }
  43. func TestErrorv(t *testing.T) {
  44. buf := logtest.NewCollector(t)
  45. file, line := getFileLine()
  46. Errorv(context.Background(), "foo")
  47. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  48. }
  49. func TestErrorw(t *testing.T) {
  50. buf := logtest.NewCollector(t)
  51. file, line := getFileLine()
  52. Errorw(context.Background(), "foo", Field("a", "b"))
  53. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  54. }
  55. func TestInfo(t *testing.T) {
  56. buf := logtest.NewCollector(t)
  57. file, line := getFileLine()
  58. Info(context.Background(), "foo")
  59. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  60. }
  61. func TestInfof(t *testing.T) {
  62. buf := logtest.NewCollector(t)
  63. file, line := getFileLine()
  64. Infof(context.Background(), "foo %s", "bar")
  65. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  66. }
  67. func TestInfov(t *testing.T) {
  68. buf := logtest.NewCollector(t)
  69. file, line := getFileLine()
  70. Infov(context.Background(), "foo")
  71. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  72. }
  73. func TestInfow(t *testing.T) {
  74. buf := logtest.NewCollector(t)
  75. file, line := getFileLine()
  76. Infow(context.Background(), "foo", Field("a", "b"))
  77. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  78. }
  79. func TestDebug(t *testing.T) {
  80. buf := logtest.NewCollector(t)
  81. file, line := getFileLine()
  82. Debug(context.Background(), "foo")
  83. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  84. }
  85. func TestDebugf(t *testing.T) {
  86. buf := logtest.NewCollector(t)
  87. file, line := getFileLine()
  88. Debugf(context.Background(), "foo %s", "bar")
  89. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  90. }
  91. func TestDebugv(t *testing.T) {
  92. buf := logtest.NewCollector(t)
  93. file, line := getFileLine()
  94. Debugv(context.Background(), "foo")
  95. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  96. }
  97. func TestDebugw(t *testing.T) {
  98. buf := logtest.NewCollector(t)
  99. file, line := getFileLine()
  100. Debugw(context.Background(), "foo", Field("a", "b"))
  101. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
  102. }
  103. func TestMust(t *testing.T) {
  104. assert.NotPanics(t, func() {
  105. Must(nil)
  106. })
  107. assert.NotPanics(t, func() {
  108. MustSetup(LogConf{})
  109. })
  110. }
  111. func TestMisc(t *testing.T) {
  112. SetLevel(logx.DebugLevel)
  113. assert.NoError(t, SetUp(LogConf{}))
  114. assert.NoError(t, Close())
  115. }
  116. func TestSlow(t *testing.T) {
  117. buf := logtest.NewCollector(t)
  118. file, line := getFileLine()
  119. Slow(context.Background(), "foo")
  120. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
  121. }
  122. func TestSlowf(t *testing.T) {
  123. buf := logtest.NewCollector(t)
  124. file, line := getFileLine()
  125. Slowf(context.Background(), "foo %s", "bar")
  126. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
  127. }
  128. func TestSlowv(t *testing.T) {
  129. buf := logtest.NewCollector(t)
  130. file, line := getFileLine()
  131. Slowv(context.Background(), "foo")
  132. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
  133. }
  134. func TestSloww(t *testing.T) {
  135. buf := logtest.NewCollector(t)
  136. file, line := getFileLine()
  137. Sloww(context.Background(), "foo", Field("a", "b"))
  138. assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
  139. }
  140. func getFileLine() (string, int) {
  141. _, file, line, _ := runtime.Caller(1)
  142. short := file
  143. for i := len(file) - 1; i > 0; i-- {
  144. if file[i] == '/' {
  145. short = file[i+1:]
  146. break
  147. }
  148. }
  149. return short, line
  150. }