github.com/klaytn/klaytn@v1.12.1/log/test_util.go (about)

     1  package log
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/klaytn/klaytn/log/term"
     9  	"github.com/mattn/go-colorable"
    10  )
    11  
    12  // Enable logging to STDERR
    13  // Exmaple use
    14  //     log.EnableLogForTest(log.LvlCrit, log.LvlTrace)
    15  // `normalLvl` is used in most cases
    16  // `verboseLvl` is used if `go test -v` flag is given
    17  func EnableLogForTest(normalLvl, verboseLvl Lvl) {
    18  	lvl := Lvl(normalLvl)
    19  	if testing.Verbose() {
    20  		lvl = Lvl(verboseLvl)
    21  	}
    22  
    23  	usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb"
    24  	output := io.Writer(os.Stderr)
    25  	if usecolor {
    26  		output = colorable.NewColorableStderr()
    27  	}
    28  
    29  	glogger := NewGlogHandler(StreamHandler(output, TerminalFormat(usecolor)))
    30  	PrintOrigins(true)
    31  	ChangeGlobalLogLevel(glogger, lvl)
    32  	glogger.Vmodule("")
    33  	glogger.BacktraceAt("")
    34  	Root().SetHandler(glogger)
    35  }