github.com/vipernet-xyz/tm@v0.34.24/libs/log/testing_logger.go (about)

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