testlog.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package testlog provides a log handler for unit tests.
  17. package testlog
  18. import (
  19. "sync"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/log"
  22. )
  23. // Handler returns a log handler which logs to the unit test log of t.
  24. func Handler(t *testing.T, level log.Lvl) log.Handler {
  25. return log.LvlFilterHandler(level, &handler{t, log.TerminalFormat(false)})
  26. }
  27. type handler struct {
  28. t *testing.T
  29. fmt log.Format
  30. }
  31. func (h *handler) Log(r *log.Record) error {
  32. h.t.Logf("%s", h.fmt.Format(r))
  33. return nil
  34. }
  35. // logger implements log.Logger such that all output goes to the unit test log via
  36. // t.Logf(). All methods in between logger.Trace, logger.Debug, etc. are marked as test
  37. // helpers, so the file and line number in unit test output correspond to the call site
  38. // which emitted the log message.
  39. type logger struct {
  40. t *testing.T
  41. l log.Logger
  42. mu *sync.Mutex
  43. h *bufHandler
  44. }
  45. type bufHandler struct {
  46. buf []*log.Record
  47. fmt log.Format
  48. }
  49. func (h *bufHandler) Log(r *log.Record) error {
  50. h.buf = append(h.buf, r)
  51. return nil
  52. }
  53. // Logger returns a logger which logs to the unit test log of t.
  54. func Logger(t *testing.T, level log.Lvl) log.Logger {
  55. l := &logger{
  56. t: t,
  57. l: log.New(),
  58. mu: new(sync.Mutex),
  59. h: &bufHandler{fmt: log.TerminalFormat(false)},
  60. }
  61. l.l.SetHandler(log.LvlFilterHandler(level, l.h))
  62. return l
  63. }
  64. func (l *logger) Trace(msg string, ctx ...interface{}) {
  65. l.t.Helper()
  66. l.mu.Lock()
  67. defer l.mu.Unlock()
  68. l.l.Trace(msg, ctx...)
  69. l.flush()
  70. }
  71. func (l *logger) Debug(msg string, ctx ...interface{}) {
  72. l.t.Helper()
  73. l.mu.Lock()
  74. defer l.mu.Unlock()
  75. l.l.Debug(msg, ctx...)
  76. l.flush()
  77. }
  78. func (l *logger) Info(msg string, ctx ...interface{}) {
  79. l.t.Helper()
  80. l.mu.Lock()
  81. defer l.mu.Unlock()
  82. l.l.Info(msg, ctx...)
  83. l.flush()
  84. }
  85. func (l *logger) Warn(msg string, ctx ...interface{}) {
  86. l.t.Helper()
  87. l.mu.Lock()
  88. defer l.mu.Unlock()
  89. l.l.Warn(msg, ctx...)
  90. l.flush()
  91. }
  92. func (l *logger) Error(msg string, ctx ...interface{}) {
  93. l.t.Helper()
  94. l.mu.Lock()
  95. defer l.mu.Unlock()
  96. l.l.Error(msg, ctx...)
  97. l.flush()
  98. }
  99. func (l *logger) Crit(msg string, ctx ...interface{}) {
  100. l.t.Helper()
  101. l.mu.Lock()
  102. defer l.mu.Unlock()
  103. l.l.Crit(msg, ctx...)
  104. l.flush()
  105. }
  106. func (l *logger) New(ctx ...interface{}) log.Logger {
  107. return &logger{l.t, l.l.New(ctx...), l.mu, l.h}
  108. }
  109. func (l *logger) GetHandler() log.Handler {
  110. return l.l.GetHandler()
  111. }
  112. func (l *logger) SetHandler(h log.Handler) {
  113. l.l.SetHandler(h)
  114. }
  115. // flush writes all buffered messages and clears the buffer.
  116. func (l *logger) flush() {
  117. l.t.Helper()
  118. for _, r := range l.h.buf {
  119. l.t.Logf("%s", l.h.fmt.Format(r))
  120. }
  121. l.h.buf = nil
  122. }