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.

41 lines
863 B

  1. package log
  2. import (
  3. "io"
  4. "os"
  5. "sync"
  6. "testing"
  7. )
  8. var (
  9. // reuse the same logger across all tests
  10. testingLoggerMtx = sync.Mutex{}
  11. testingLogger Logger
  12. )
  13. // TestingLogger returns a Logger which writes to STDOUT if test(s) are being
  14. // run with the verbose (-v) flag, NopLogger otherwise.
  15. //
  16. // NOTE:
  17. // - A call to NewTestingLogger() must be made inside a test (not in the init func)
  18. // because verbose flag only set at the time of testing.
  19. func TestingLogger() Logger {
  20. return TestingLoggerWithOutput(os.Stdout)
  21. }
  22. func TestingLoggerWithOutput(w io.Writer) Logger {
  23. testingLoggerMtx.Lock()
  24. defer testingLoggerMtx.Unlock()
  25. if testingLogger != nil {
  26. return testingLogger
  27. }
  28. if testing.Verbose() {
  29. testingLogger = MustNewDefaultLogger(LogFormatText, LogLevelDebug, true)
  30. } else {
  31. testingLogger = NewNopLogger()
  32. }
  33. return testingLogger
  34. }