logging_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package util
  11. import (
  12. "bytes"
  13. "fmt"
  14. "testing"
  15. )
  16. func TestLogging(t *testing.T) {
  17. ml := NewMemoryLogger(5)
  18. ml.LogDebug("test")
  19. ml.LogInfo("test")
  20. if ml.String() != `debug: test
  21. test` {
  22. t.Error("Unexpected result:", ml.String())
  23. return
  24. }
  25. if res := fmt.Sprint(ml.Slice()); res != "[debug: test test]" {
  26. t.Error("Unexpected result:", res)
  27. return
  28. }
  29. ml.Reset()
  30. ml.LogError("test1")
  31. if res := fmt.Sprint(ml.Slice()); res != "[error: test1]" {
  32. t.Error("Unexpected result:", res)
  33. return
  34. }
  35. if res := ml.Size(); res != 1 {
  36. t.Error("Unexpected result:", res)
  37. return
  38. }
  39. // Test that the functions can be called
  40. nl := NewNullLogger()
  41. nl.LogDebug(nil, "test")
  42. nl.LogInfo(nil, "test")
  43. nl.LogError(nil, "test")
  44. sol := NewStdOutLogger()
  45. sol.stdlog = func(v ...interface{}) {}
  46. sol.LogDebug(nil, "test")
  47. sol.LogInfo(nil, "test")
  48. sol.LogError(nil, "test")
  49. ml.Reset()
  50. if _, err := NewLogLevelLogger(ml, "test"); err == nil || err.Error() != "Invalid log level: test" {
  51. t.Error("Unexpected result:", err)
  52. return
  53. }
  54. ml.Reset()
  55. ll, _ := NewLogLevelLogger(ml, "debug")
  56. ll.LogDebug("l", "test1")
  57. ll.LogInfo(nil, "test2")
  58. ll.LogError("l", "test3")
  59. if ml.String() != `debug: ltest1
  60. <nil>test2
  61. error: ltest3` {
  62. t.Error("Unexpected result:", ml.String())
  63. return
  64. }
  65. ml.Reset()
  66. ll, _ = NewLogLevelLogger(ml, "info")
  67. ll.LogDebug("l", "test1")
  68. ll.LogInfo(nil, "test2")
  69. ll.LogError("l", "test3")
  70. if ml.String() != `<nil>test2
  71. error: ltest3` {
  72. t.Error("Unexpected result:", ml.String())
  73. return
  74. }
  75. ml.Reset()
  76. ll, _ = NewLogLevelLogger(ml, "error")
  77. if ll.Level() != "error" {
  78. t.Error("Unexpected level:", ll.Level())
  79. return
  80. }
  81. ll.LogDebug("l", "test1")
  82. ll.LogInfo(nil, "test2")
  83. ll.LogError("l", "test3")
  84. if ml.String() != `error: ltest3` {
  85. t.Error("Unexpected result:", ml.String())
  86. return
  87. }
  88. buf := bytes.NewBuffer(nil)
  89. bl := NewBufferLogger(buf)
  90. bl.LogDebug("l", "test1")
  91. bl.LogInfo(nil, "test2")
  92. bl.LogError("l", "test3")
  93. if buf.String() != `debug: ltest1
  94. <nil>test2
  95. error: ltest3
  96. ` {
  97. t.Error("Unexpected result:", buf.String())
  98. return
  99. }
  100. }