tracelogger_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package logx
  2. import (
  3. "context"
  4. "log"
  5. "strings"
  6. "sync/atomic"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/tal-tech/go-zero/core/trace/tracespec"
  11. )
  12. const (
  13. mockTraceID = "mock-trace-id"
  14. mockSpanID = "mock-span-id"
  15. )
  16. var mock tracespec.Trace = new(mockTrace)
  17. func TestTraceLog(t *testing.T) {
  18. var buf mockWriter
  19. atomic.StoreUint32(&initialized, 1)
  20. ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
  21. WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
  22. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  23. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  24. }
  25. func TestTraceError(t *testing.T) {
  26. var buf mockWriter
  27. atomic.StoreUint32(&initialized, 1)
  28. errorLog = newLogWriter(log.New(&buf, "", flags))
  29. ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
  30. l := WithContext(ctx).(*traceLogger)
  31. SetLevel(InfoLevel)
  32. l.WithDuration(time.Second).Error(testlog)
  33. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  34. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  35. buf.Reset()
  36. l.WithDuration(time.Second).Errorf(testlog)
  37. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  38. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  39. }
  40. func TestTraceInfo(t *testing.T) {
  41. var buf mockWriter
  42. atomic.StoreUint32(&initialized, 1)
  43. infoLog = newLogWriter(log.New(&buf, "", flags))
  44. ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
  45. l := WithContext(ctx).(*traceLogger)
  46. SetLevel(InfoLevel)
  47. l.WithDuration(time.Second).Info(testlog)
  48. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  49. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  50. buf.Reset()
  51. l.WithDuration(time.Second).Infof(testlog)
  52. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  53. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  54. }
  55. func TestTraceSlow(t *testing.T) {
  56. var buf mockWriter
  57. atomic.StoreUint32(&initialized, 1)
  58. slowLog = newLogWriter(log.New(&buf, "", flags))
  59. ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
  60. l := WithContext(ctx).(*traceLogger)
  61. SetLevel(InfoLevel)
  62. l.WithDuration(time.Second).Slow(testlog)
  63. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  64. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  65. buf.Reset()
  66. l.WithDuration(time.Second).Slowf(testlog)
  67. assert.True(t, strings.Contains(buf.String(), mockTraceID))
  68. assert.True(t, strings.Contains(buf.String(), mockSpanID))
  69. }
  70. func TestTraceWithoutContext(t *testing.T) {
  71. var buf mockWriter
  72. atomic.StoreUint32(&initialized, 1)
  73. infoLog = newLogWriter(log.New(&buf, "", flags))
  74. l := WithContext(context.Background()).(*traceLogger)
  75. SetLevel(InfoLevel)
  76. l.WithDuration(time.Second).Info(testlog)
  77. assert.False(t, strings.Contains(buf.String(), mockTraceID))
  78. assert.False(t, strings.Contains(buf.String(), mockSpanID))
  79. buf.Reset()
  80. l.WithDuration(time.Second).Infof(testlog)
  81. assert.False(t, strings.Contains(buf.String(), mockTraceID))
  82. assert.False(t, strings.Contains(buf.String(), mockSpanID))
  83. }
  84. type mockTrace struct{}
  85. func (t mockTrace) TraceId() string {
  86. return mockTraceID
  87. }
  88. func (t mockTrace) SpanId() string {
  89. return mockSpanID
  90. }
  91. func (t mockTrace) Finish() {
  92. }
  93. func (t mockTrace) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
  94. return nil, nil
  95. }
  96. func (t mockTrace) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
  97. return nil, nil
  98. }
  99. func (t mockTrace) Visit(fn func(key string, val string) bool) {
  100. }