You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

  1. package log
  2. import (
  3. "io"
  4. "os"
  5. "testing"
  6. "github.com/go-kit/kit/log/term"
  7. )
  8. var (
  9. // reuse the same logger across all tests
  10. _testingLogger Logger
  11. )
  12. // TestingLogger returns a TMLogger which writes to STDOUT if testing being run
  13. // with the verbose (-v) flag, NopLogger otherwise.
  14. //
  15. // Note that the call to TestingLogger() must be made
  16. // inside a test (not in the init func) because
  17. // verbose flag only set at the time of testing.
  18. func TestingLogger() Logger {
  19. return TestingLoggerWithOutput(os.Stdout)
  20. }
  21. // TestingLoggerWOutput returns a TMLogger which writes to (w io.Writer) if testing being run
  22. // with the verbose (-v) flag, NopLogger otherwise.
  23. //
  24. // Note that the call to TestingLoggerWithOutput(w io.Writer) must be made
  25. // inside a test (not in the init func) because
  26. // verbose flag only set at the time of testing.
  27. func TestingLoggerWithOutput(w io.Writer) Logger {
  28. if _testingLogger != nil {
  29. return _testingLogger
  30. }
  31. if testing.Verbose() {
  32. _testingLogger = NewTMLogger(NewSyncWriter(w))
  33. } else {
  34. _testingLogger = NewNopLogger()
  35. }
  36. return _testingLogger
  37. }
  38. // TestingLoggerWithColorFn allow you to provide your own color function. See
  39. // TestingLogger for documentation.
  40. func TestingLoggerWithColorFn(colorFn func(keyvals ...interface{}) term.FgBgColor) Logger {
  41. if _testingLogger != nil {
  42. return _testingLogger
  43. }
  44. if testing.Verbose() {
  45. _testingLogger = NewTMLoggerWithColorFn(NewSyncWriter(os.Stdout), colorFn)
  46. } else {
  47. _testingLogger = NewNopLogger()
  48. }
  49. return _testingLogger
  50. }