durationlogger_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package logx
  2. import (
  3. "log"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestWithDurationError(t *testing.T) {
  10. var builder strings.Builder
  11. log.SetOutput(&builder)
  12. WithDuration(time.Second).Error("foo")
  13. assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
  14. }
  15. func TestWithDurationErrorf(t *testing.T) {
  16. var builder strings.Builder
  17. log.SetOutput(&builder)
  18. WithDuration(time.Second).Errorf("foo")
  19. assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
  20. }
  21. func TestWithDurationInfo(t *testing.T) {
  22. var builder strings.Builder
  23. log.SetOutput(&builder)
  24. WithDuration(time.Second).Info("foo")
  25. assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
  26. }
  27. func TestWithDurationInfof(t *testing.T) {
  28. var builder strings.Builder
  29. log.SetOutput(&builder)
  30. WithDuration(time.Second).Infof("foo")
  31. assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
  32. }
  33. func TestWithDurationSlow(t *testing.T) {
  34. var builder strings.Builder
  35. log.SetOutput(&builder)
  36. WithDuration(time.Second).Slow("foo")
  37. assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
  38. }
  39. func TestWithDurationSlowf(t *testing.T) {
  40. var builder strings.Builder
  41. log.SetOutput(&builder)
  42. WithDuration(time.Second).WithDuration(time.Hour).Slowf("foo")
  43. assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
  44. }