formatter_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package logutil
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. )
  7. func TestFormatting(t *testing.T) {
  8. ClearLogSinks()
  9. sf := TemplateFormatter("%t [%l] %s %m")
  10. sf.(*templateFormatter).tsFunc = func() string {
  11. return "0000000000000" // Timestamp for testing is always 0
  12. }
  13. rootBuf := &bytes.Buffer{}
  14. logger := GetLogger("")
  15. logger.AddLogSink(Debug, sf, rootBuf)
  16. logger.Info("foo")
  17. logger.Warning("bar")
  18. if rootBuf.String() != `
  19. 0000000000000 [Info] foo
  20. 0000000000000 [Warning] bar
  21. `[1:] {
  22. t.Error("Unexpected output:", rootBuf.String())
  23. return
  24. }
  25. ClearLogSinks()
  26. sf = TemplateFormatter("%c - %m")
  27. sf.(*templateFormatter).tsFunc = func() string {
  28. return "0000000000000" // Timestamp for testing is always 0
  29. }
  30. rootBuf = &bytes.Buffer{}
  31. logger = GetLogger("")
  32. logger.AddLogSink(Debug, sf, rootBuf)
  33. logger.Info("foo")
  34. logger.Warning("bar")
  35. if !strings.Contains(rootBuf.String(), "formatter_test.go:47") {
  36. t.Error("Unexpected output:", rootBuf.String())
  37. return
  38. }
  39. ClearLogSinks()
  40. rootBuf = &bytes.Buffer{}
  41. logger = GetLogger("")
  42. logger.AddLogSink(Debug, ConsoleFormatter(), rootBuf)
  43. logger.Info("foo")
  44. logger.Warning("bar")
  45. if rootBuf.String() != `
  46. Info: foo
  47. Warning: bar
  48. `[1:] {
  49. t.Error("Unexpected output:", rootBuf.String())
  50. return
  51. }
  52. }