logger_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2014 Jakob Borg. All rights reserved. Use of this source code
  2. // is governed by an MIT-style license that can be found in the LICENSE file.
  3. package logger
  4. import (
  5. "fmt"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. func TestAPI(t *testing.T) {
  11. l := New()
  12. l.SetFlags(0)
  13. l.SetPrefix("testing")
  14. debug := 0
  15. l.AddHandler(LevelDebug, checkFunc(t, LevelDebug, &debug))
  16. info := 0
  17. l.AddHandler(LevelInfo, checkFunc(t, LevelInfo, &info))
  18. warn := 0
  19. l.AddHandler(LevelWarn, checkFunc(t, LevelWarn, &warn))
  20. l.Debugf("test %d", 0)
  21. l.Debugln("test", 0)
  22. l.Infof("test %d", 1)
  23. l.Infoln("test", 1)
  24. l.Warnf("test %d", 3)
  25. l.Warnln("test", 3)
  26. if debug != 6 {
  27. t.Errorf("Debug handler called %d != 8 times", debug)
  28. }
  29. if info != 4 {
  30. t.Errorf("Info handler called %d != 6 times", info)
  31. }
  32. if warn != 2 {
  33. t.Errorf("Warn handler called %d != 2 times", warn)
  34. }
  35. }
  36. func checkFunc(t *testing.T, expectl LogLevel, counter *int) func(LogLevel, string) {
  37. return func(l LogLevel, msg string) {
  38. *counter++
  39. if l < expectl {
  40. t.Errorf("Incorrect message level %d < %d", l, expectl)
  41. }
  42. }
  43. }
  44. func TestFacilityDebugging(t *testing.T) {
  45. l := New()
  46. l.SetFlags(0)
  47. msgs := 0
  48. l.AddHandler(LevelDebug, func(l LogLevel, msg string) {
  49. msgs++
  50. if strings.Contains(msg, "f1") {
  51. t.Fatal("Should not get message for facility f1")
  52. }
  53. })
  54. f0 := l.NewFacility("f0", "foo#0")
  55. f1 := l.NewFacility("f1", "foo#1")
  56. l.SetDebug("f0", true)
  57. l.SetDebug("f1", false)
  58. f0.Debugln("Debug line from f0")
  59. f1.Debugln("Debug line from f1")
  60. if msgs != 1 {
  61. t.Fatalf("Incorrect number of messages, %d != 1", msgs)
  62. }
  63. }
  64. func TestRecorder(t *testing.T) {
  65. l := New()
  66. l.SetFlags(0)
  67. // Keep the last five warnings or higher, no special initial handling.
  68. r0 := NewRecorder(l, LevelWarn, 5, 0)
  69. // Keep the last ten infos or higher, with the first three being permanent.
  70. r1 := NewRecorder(l, LevelInfo, 10, 3)
  71. // Log a bunch of messages.
  72. for i := 0; i < 15; i++ {
  73. l.Debugf("Debug#%d", i)
  74. l.Infof("Info#%d", i)
  75. l.Warnf("Warn#%d", i)
  76. }
  77. // r0 should contain the last five warnings
  78. lines := r0.Since(time.Time{})
  79. if len(lines) != 5 {
  80. t.Fatalf("Incorrect length %d != 5", len(lines))
  81. }
  82. for i := 0; i < 5; i++ {
  83. expected := fmt.Sprintf("Warn#%d", i+10)
  84. if lines[i].Message != expected {
  85. t.Error("Incorrect warning in r0:", lines[i].Message, "!=", expected)
  86. }
  87. }
  88. // r0 should contain:
  89. // - The first three messages
  90. // - A "..." marker
  91. // - The last six messages
  92. // (totalling ten)
  93. lines = r1.Since(time.Time{})
  94. if len(lines) != 10 {
  95. t.Fatalf("Incorrect length %d != 10", len(lines))
  96. }
  97. expected := []string{
  98. "Info#0",
  99. "Warn#0",
  100. "Info#1",
  101. "...",
  102. "Info#12",
  103. "Warn#12",
  104. "Info#13",
  105. "Warn#13",
  106. "Info#14",
  107. "Warn#14",
  108. }
  109. for i := 0; i < 10; i++ {
  110. if lines[i].Message != expected[i] {
  111. t.Error("Incorrect warning in r0:", lines[i].Message, "!=", expected[i])
  112. }
  113. }
  114. // Check that since works
  115. now := time.Now()
  116. time.Sleep(time.Millisecond)
  117. lines = r1.Since(now)
  118. if len(lines) != 0 {
  119. t.Error("unexpected lines")
  120. }
  121. l.Infoln("hah")
  122. lines = r1.Since(now)
  123. if len(lines) != 1 {
  124. t.Fatalf("unexpected line count: %d", len(lines))
  125. }
  126. if lines[0].Message != "hah" {
  127. t.Errorf("incorrect line: %s", lines[0].Message)
  128. }
  129. }