logger_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. ok := 0
  19. l.AddHandler(LevelOK, checkFunc(t, LevelOK, &ok))
  20. warn := 0
  21. l.AddHandler(LevelWarn, checkFunc(t, LevelWarn, &warn))
  22. l.Debugf("test %d", 0)
  23. l.Debugln("test", 0)
  24. l.Infof("test %d", 1)
  25. l.Infoln("test", 1)
  26. l.Okf("test %d", 2)
  27. l.Okln("test", 2)
  28. l.Warnf("test %d", 3)
  29. l.Warnln("test", 3)
  30. if debug != 8 {
  31. t.Errorf("Debug handler called %d != 8 times", debug)
  32. }
  33. if info != 6 {
  34. t.Errorf("Info handler called %d != 6 times", info)
  35. }
  36. if ok != 4 {
  37. t.Errorf("Ok handler called %d != 4 times", ok)
  38. }
  39. if warn != 2 {
  40. t.Errorf("Warn handler called %d != 2 times", warn)
  41. }
  42. }
  43. func checkFunc(t *testing.T, expectl LogLevel, counter *int) func(LogLevel, string) {
  44. return func(l LogLevel, msg string) {
  45. *counter++
  46. if l < expectl {
  47. t.Errorf("Incorrect message level %d < %d", l, expectl)
  48. }
  49. }
  50. }
  51. func TestFacilityDebugging(t *testing.T) {
  52. l := New()
  53. l.SetFlags(0)
  54. msgs := 0
  55. l.AddHandler(LevelDebug, func(l LogLevel, msg string) {
  56. msgs++
  57. if strings.Contains(msg, "f1") {
  58. t.Fatal("Should not get message for facility f1")
  59. }
  60. })
  61. f0 := l.NewFacility("f0", "foo#0")
  62. f1 := l.NewFacility("f1", "foo#1")
  63. l.SetDebug("f0", true)
  64. l.SetDebug("f1", false)
  65. f0.Debugln("Debug line from f0")
  66. f1.Debugln("Debug line from f1")
  67. if msgs != 1 {
  68. t.Fatalf("Incorrent number of messages, %d != 1", msgs)
  69. }
  70. }
  71. func TestRecorder(t *testing.T) {
  72. l := New()
  73. l.SetFlags(0)
  74. // Keep the last five warnings or higher, no special initial handling.
  75. r0 := NewRecorder(l, LevelWarn, 5, 0)
  76. // Keep the last ten infos or higher, with the first three being permanent.
  77. r1 := NewRecorder(l, LevelInfo, 10, 3)
  78. // Log a bunch of messages.
  79. for i := 0; i < 15; i++ {
  80. l.Debugf("Debug#%d", i)
  81. l.Infof("Info#%d", i)
  82. l.Warnf("Warn#%d", i)
  83. }
  84. // r0 should contain the last five warnings
  85. lines := r0.Since(time.Time{})
  86. if len(lines) != 5 {
  87. t.Fatalf("Incorrect length %d != 5", len(lines))
  88. }
  89. for i := 0; i < 5; i++ {
  90. expected := fmt.Sprintf("Warn#%d", i+10)
  91. if lines[i].Message != expected {
  92. t.Error("Incorrect warning in r0:", lines[i].Message, "!=", expected)
  93. }
  94. }
  95. // r0 should contain:
  96. // - The first three messages
  97. // - A "..." marker
  98. // - The last six messages
  99. // (totalling ten)
  100. lines = r1.Since(time.Time{})
  101. if len(lines) != 10 {
  102. t.Fatalf("Incorrect length %d != 10", len(lines))
  103. }
  104. expected := []string{
  105. "Info#0",
  106. "Warn#0",
  107. "Info#1",
  108. "...",
  109. "Info#12",
  110. "Warn#12",
  111. "Info#13",
  112. "Warn#13",
  113. "Info#14",
  114. "Warn#14",
  115. }
  116. for i := 0; i < 10; i++ {
  117. if lines[i].Message != expected[i] {
  118. t.Error("Incorrect warning in r0:", lines[i].Message, "!=", expected[i])
  119. }
  120. }
  121. }