logger_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package logutil
  10. import (
  11. "bytes"
  12. "fmt"
  13. "strings"
  14. "testing"
  15. )
  16. type brokenSink struct {
  17. }
  18. func (bs *brokenSink) Write(p []byte) (n int, err error) {
  19. return 0, fmt.Errorf("testerror")
  20. }
  21. func TestLogging(t *testing.T) {
  22. if StringToLoglevel("iNfO") != Info {
  23. t.Error("Unexpected result")
  24. return
  25. }
  26. ClearLogSinks()
  27. sf := SimpleFormatter()
  28. sf.(*simpleFormatter).tsFunc = func() string {
  29. return "0000000000000" // Timestamp for testing is always 0
  30. }
  31. // Test straight forward case doing root logging
  32. rootBuf := &bytes.Buffer{}
  33. logger := GetLogger("")
  34. logger.AddLogSink(Debug, sf, rootBuf)
  35. logger.Info("foo")
  36. logger.Warning("bar")
  37. if rootBuf.String() != `
  38. 0000000000000 Info foo
  39. 0000000000000 Warning bar
  40. `[1:] {
  41. t.Error("Unexpected output:", rootBuf.String())
  42. return
  43. }
  44. logger.LogStackTrace(Error, "test123")
  45. logger.Warning("next")
  46. if !strings.Contains(rootBuf.String(), "logger_test.go") {
  47. t.Error("Unexpected output:", rootBuf.String())
  48. return
  49. }
  50. rootBuf.Reset()
  51. logger.Info("foo")
  52. logger.Warning("bar")
  53. // Add a sub package logger
  54. subBuf := &bytes.Buffer{}
  55. logger = GetLogger("foo")
  56. logger.AddLogSink(Info, sf, subBuf)
  57. logger.Debug("debugmsg")
  58. logger.Info("foo")
  59. logger.Warning("bar")
  60. // Debug message was handled in root logger
  61. if rootBuf.String() != `
  62. 0000000000000 Info foo
  63. 0000000000000 Warning bar
  64. 0000000000000 Debug foo debugmsg
  65. `[1:] {
  66. t.Error("Unexpected output:", rootBuf.String())
  67. return
  68. }
  69. // Info and warning where handled in the sub logger
  70. if subBuf.String() != `
  71. 0000000000000 Info foo foo
  72. 0000000000000 Warning foo bar
  73. `[1:] {
  74. t.Error("Unexpected output:", subBuf.String())
  75. return
  76. }
  77. // Add a sub sub package logger
  78. subsubBuf := &bytes.Buffer{}
  79. logger = GetLogger("foo.bar")
  80. // Add the logger twice
  81. logger.AddLogSink(Error, sf, subsubBuf)
  82. logger.AddLogSink(Error, sf, subsubBuf)
  83. logger = GetLogger("foo.bar.bla")
  84. logger.Error("test1")
  85. logger.Info("test2")
  86. logger.Debug("test3")
  87. // Check that the messages were distributed correctly
  88. if rootBuf.String() != `
  89. 0000000000000 Info foo
  90. 0000000000000 Warning bar
  91. 0000000000000 Debug foo debugmsg
  92. 0000000000000 Debug foo.bar.bla test3
  93. `[1:] {
  94. t.Error("Unexpected output:", rootBuf.String())
  95. return
  96. }
  97. if subBuf.String() != `
  98. 0000000000000 Info foo foo
  99. 0000000000000 Warning foo bar
  100. 0000000000000 Info foo.bar.bla test2
  101. `[1:] {
  102. t.Error("Unexpected output:", subBuf.String())
  103. return
  104. }
  105. // Log message is duplicated as we have the same sink twice
  106. if subsubBuf.String() != `
  107. 0000000000000 Error foo.bar.bla test1
  108. 0000000000000 Error foo.bar.bla test1
  109. `[1:] {
  110. t.Error("Unexpected output:", subsubBuf.String())
  111. return
  112. }
  113. // Remove all log sinks and test error cases
  114. ClearLogSinks()
  115. fallbackBuf := &bytes.Buffer{}
  116. fallbackLogger = func(v ...interface{}) {
  117. fallbackBuf.WriteString(fmt.Sprint(v...))
  118. }
  119. logger = GetLogger("foo.bar.bla")
  120. logger.Error("test1")
  121. if !strings.Contains(fallbackBuf.String(), "Error foo.bar.bla test1") {
  122. t.Error("Unexpected output:", fallbackBuf.String())
  123. return
  124. }
  125. logger = GetLogger("foo.bar")
  126. logger.AddLogSink(Info, sf, &brokenSink{})
  127. logger.Info("test")
  128. if !strings.Contains(fallbackBuf.String(), "testerror") {
  129. t.Error("Unexpected output:", fallbackBuf.String())
  130. return
  131. }
  132. }