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.

49 lines
1.1 KiB

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