gotest.tools/gotestsum@v1.11.0/internal/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/fatih/color"
     7  )
     8  
     9  type Level uint8
    10  
    11  const (
    12  	ErrorLevel Level = iota
    13  	WarnLevel
    14  	InfoLevel
    15  	DebugLevel
    16  )
    17  
    18  var (
    19  	level = WarnLevel
    20  	out   = color.Error
    21  )
    22  
    23  // SetLevel for the global logger.
    24  func SetLevel(l Level) {
    25  	level = l
    26  }
    27  
    28  // Warnf prints the message to stderr, with a yellow WARN prefix.
    29  func Warnf(format string, args ...interface{}) {
    30  	if level < WarnLevel {
    31  		return
    32  	}
    33  	fmt.Fprint(out, color.YellowString("WARN "))
    34  	fmt.Fprintf(out, format, args...)
    35  	fmt.Fprint(out, "\n")
    36  }
    37  
    38  // Debugf prints the message to stderr, with no prefix.
    39  func Debugf(format string, args ...interface{}) {
    40  	if level < DebugLevel {
    41  		return
    42  	}
    43  	fmt.Fprintf(out, format, args...)
    44  	fmt.Fprint(out, "\n")
    45  }
    46  
    47  // Infof prints the message to stderr, with no prefix.
    48  func Infof(format string, args ...interface{}) {
    49  	if level < InfoLevel {
    50  		return
    51  	}
    52  	fmt.Fprintf(out, format, args...)
    53  	fmt.Fprint(out, "\n")
    54  }
    55  
    56  // Errorf prints the message to stderr, with a red ERROR prefix.
    57  func Errorf(format string, args ...interface{}) {
    58  	if level < ErrorLevel {
    59  		return
    60  	}
    61  	fmt.Fprint(out, color.RedString("ERROR "))
    62  	fmt.Fprintf(out, format, args...)
    63  	fmt.Fprint(out, "\n")
    64  }
    65  
    66  // Error prints the message to stderr, with a red ERROR prefix.
    67  func Error(msg string) {
    68  	if level < ErrorLevel {
    69  		return
    70  	}
    71  	fmt.Fprint(out, color.RedString("ERROR "))
    72  	fmt.Fprintln(out, msg)
    73  }