log_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package fslog
  2. import (
  3. "bytes"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. )
  9. func TestLevel(t *testing.T) {
  10. defer Flush()
  11. SetLv(DebugLv)
  12. buffer := &bytes.Buffer{}
  13. NewOutput(buffer)
  14. if Lv() != DebugLv {
  15. t.Error("level not match")
  16. }
  17. Info("info message")
  18. out := buffer.String()
  19. if !strings.Contains(out, "info message") {
  20. t.Error("lower level cannot log higher level message")
  21. }
  22. SetLv(ErrorLv)
  23. defer SetLv(DebugLv)
  24. if Lv() != ErrorLv {
  25. t.Error("level not match")
  26. }
  27. Debug("debug message")
  28. out = buffer.String()
  29. if strings.Contains(out, "debug message") {
  30. t.Error("higher level can log lower level message")
  31. }
  32. }
  33. func TestDebug(t *testing.T) {
  34. defer Flush()
  35. SetLv(DebugLv)
  36. buffer := &bytes.Buffer{}
  37. NewOutput(buffer)
  38. Debug("test debug %s", "fmt")
  39. out := buffer.String()
  40. if !strings.Contains(out, `"level":"debug"`) {
  41. t.Error("debug level not match")
  42. }
  43. if !strings.Contains(out, "test debug") {
  44. t.Error("debug message not match")
  45. }
  46. if !strings.Contains(out, "fmt") {
  47. t.Error("debug fmt message not match")
  48. }
  49. if !strings.Contains(out, "test debug fmt") {
  50. t.Error("debug all message not match")
  51. }
  52. if !strings.Contains(out, "logger.go") {
  53. t.Error("debug caller not match")
  54. }
  55. }
  56. func TestInfo(t *testing.T) {
  57. defer Flush()
  58. SetLv(DebugLv)
  59. buffer := &bytes.Buffer{}
  60. NewOutput(buffer)
  61. Info("test info %s", "fmt")
  62. out := buffer.String()
  63. if !strings.Contains(out, `"level":"info"`) {
  64. t.Error("info level not match")
  65. }
  66. if !strings.Contains(out, "test info") {
  67. t.Error("info message not match")
  68. }
  69. if !strings.Contains(out, "fmt") {
  70. t.Error("info fmt message not match")
  71. }
  72. if !strings.Contains(out, "test info fmt") {
  73. t.Error("info fmt all message not match")
  74. }
  75. if !strings.Contains(out, "logger.go") {
  76. t.Error("info caller not match")
  77. }
  78. }
  79. func TestWarn(t *testing.T) {
  80. defer Flush()
  81. SetLv(DebugLv)
  82. buffer := &bytes.Buffer{}
  83. NewOutput(buffer)
  84. Warn("test warn %s", "fmt")
  85. out := buffer.String()
  86. if !strings.Contains(out, `"level":"warn"`) {
  87. t.Error("warn level not match")
  88. }
  89. if !strings.Contains(out, "test warn") {
  90. t.Error("warn message not match")
  91. }
  92. if !strings.Contains(out, "fmt") {
  93. t.Error("warn fmt message not match")
  94. }
  95. if !strings.Contains(out, "test warn fmt") {
  96. t.Error("warn fmt all message not match")
  97. }
  98. if !strings.Contains(out, "logger.go") {
  99. t.Error("warn caller not match")
  100. }
  101. }
  102. func TestError(t *testing.T) {
  103. defer Flush()
  104. SetLv(DebugLv)
  105. buffer := &bytes.Buffer{}
  106. NewOutput(buffer)
  107. Error("test error %s", "fmt")
  108. out := buffer.String()
  109. if !strings.Contains(out, `"level":"error"`) {
  110. t.Error("error level not match")
  111. }
  112. if !strings.Contains(out, "test error") {
  113. t.Error("error message not match")
  114. }
  115. if !strings.Contains(out, "fmt") {
  116. t.Error("error fmt message not match")
  117. }
  118. if !strings.Contains(out, "test error fmt") {
  119. t.Error("error fmt all message not match")
  120. }
  121. if !strings.Contains(out, "logger.go") {
  122. t.Error("error caller not match")
  123. }
  124. }
  125. func TestWith(t *testing.T) {
  126. defer Flush()
  127. buffer := &bytes.Buffer{}
  128. NewOutput(buffer)
  129. With("key", "value").Info("test with")
  130. out := strings.Trim(buffer.String(), " ")
  131. if !strings.Contains(out, `"key":"value"`) {
  132. t.Error("with key value not match")
  133. }
  134. }
  135. func TestNewFileOutput(t *testing.T) {
  136. defer Flush()
  137. SetLv(DebugLv)
  138. NewFileOutput(WithFilename("logs/test"))
  139. Debug("test new file output")
  140. contentBytes, err := os.ReadFile("logs/test")
  141. if err != nil {
  142. t.Error("read file error", err)
  143. }
  144. content := string(contentBytes)
  145. if !strings.Contains(content, "test new file output") {
  146. t.Error("file output content not match")
  147. }
  148. }
  149. func TestFileExtend(t *testing.T) {
  150. for i := 0; i < 100000; i++ {
  151. Info(strconv.Itoa(i))
  152. }
  153. }