log_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package log
  2. import (
  3. "bytes"
  4. "log"
  5. "strings"
  6. "testing"
  7. )
  8. const teststring = "asdf123"
  9. func TestOutputf(t *testing.T) {
  10. buf := new(bytes.Buffer)
  11. log.SetOutput(buf)
  12. Level = LevelDebug
  13. outputf(LevelDebug, teststring, nil)
  14. // outputf correctly prints string
  15. if !strings.Contains(buf.String(), teststring) {
  16. t.Fail()
  17. }
  18. return
  19. }
  20. func TestOutput(t *testing.T) {
  21. buf := new(bytes.Buffer)
  22. log.SetOutput(buf)
  23. Level = LevelDebug
  24. output(LevelDebug, nil)
  25. // outputf correctly prints string with proper Debug prefix
  26. if !strings.Contains(buf.String(), levelPrefix[LevelDebug]) {
  27. t.Fail()
  28. }
  29. return
  30. }
  31. func TestCriticalf(t *testing.T) {
  32. buf := new(bytes.Buffer)
  33. log.SetOutput(buf)
  34. Criticalf(teststring, nil)
  35. // outputf correctly prints string
  36. // should never fail because critical > debug
  37. if !strings.Contains(buf.String(), teststring) {
  38. t.Fail()
  39. }
  40. return
  41. }
  42. func TestCritical(t *testing.T) {
  43. buf := new(bytes.Buffer)
  44. log.SetOutput(buf)
  45. Critical(nil)
  46. // outputf correctly prints string
  47. if !strings.Contains(buf.String(), levelPrefix[LevelCritical]) {
  48. t.Fail()
  49. }
  50. return
  51. }
  52. func TestWarningf(t *testing.T) {
  53. buf := new(bytes.Buffer)
  54. log.SetOutput(buf)
  55. Warningf(teststring, nil)
  56. // outputf correctly prints string
  57. // should never fail because fatal critical > debug
  58. if !strings.Contains(buf.String(), teststring) {
  59. t.Fail()
  60. }
  61. return
  62. }
  63. func TestWarning(t *testing.T) {
  64. buf := new(bytes.Buffer)
  65. log.SetOutput(buf)
  66. Warning(nil)
  67. // outputf correctly prints string
  68. if !strings.Contains(buf.String(), levelPrefix[LevelWarning]) {
  69. t.Fail()
  70. }
  71. return
  72. }
  73. func TestInfof(t *testing.T) {
  74. buf := new(bytes.Buffer)
  75. log.SetOutput(buf)
  76. Infof(teststring, nil)
  77. // outputf correctly prints string
  78. // should never fail because fatal info > debug
  79. if !strings.Contains(buf.String(), teststring) {
  80. t.Fail()
  81. }
  82. return
  83. }
  84. func TestInfo(t *testing.T) {
  85. buf := new(bytes.Buffer)
  86. log.SetOutput(buf)
  87. Info(nil)
  88. // outputf correctly prints string
  89. if !strings.Contains(buf.String(), levelPrefix[LevelInfo]) {
  90. t.Fail()
  91. }
  92. return
  93. }
  94. func TestDebugf(t *testing.T) {
  95. buf := new(bytes.Buffer)
  96. log.SetOutput(buf)
  97. Level = LevelDebug
  98. Debugf(teststring, nil)
  99. // outputf correctly prints string
  100. // should never fail because fatal debug >= debug
  101. if !strings.Contains(buf.String(), teststring) {
  102. t.Fail()
  103. }
  104. return
  105. }
  106. func TestDebug(t *testing.T) {
  107. buf := new(bytes.Buffer)
  108. log.SetOutput(buf)
  109. Level = LevelDebug
  110. Debug(nil)
  111. // outputf correctly prints string
  112. if !strings.Contains(buf.String(), levelPrefix[LevelDebug]) {
  113. t.Fail()
  114. }
  115. return
  116. }
  117. type testSyslogger struct {
  118. *bytes.Buffer
  119. }
  120. func (l testSyslogger) Debug(s string) {
  121. l.WriteString("[DEBUG] ")
  122. _, _ = l.WriteString(s)
  123. }
  124. func (l testSyslogger) Info(s string) {
  125. l.WriteString("[INFO] ")
  126. _, _ = l.WriteString(s)
  127. }
  128. func (l testSyslogger) Warning(s string) {
  129. l.WriteString("[WARN] ")
  130. _, _ = l.WriteString(s)
  131. }
  132. func (l testSyslogger) Err(s string) {
  133. l.WriteString("[ERROR] ")
  134. _, _ = l.WriteString(s)
  135. }
  136. func (l testSyslogger) Crit(s string) {
  137. l.WriteString("[CRIT] ")
  138. _, _ = l.WriteString(s)
  139. }
  140. func (l testSyslogger) Emerg(s string) {
  141. l.WriteString("[FATAL] ")
  142. _, _ = l.WriteString(s)
  143. }
  144. func TestSetLogger(t *testing.T) {
  145. buf := new(bytes.Buffer)
  146. SetLogger(testSyslogger{buf})
  147. Level = LevelDebug
  148. outputf(LevelDebug, teststring, nil)
  149. // outputf correctly prints string
  150. if !strings.Contains(buf.String(), teststring) {
  151. t.Fail()
  152. }
  153. SetLogger(nil)
  154. return
  155. }