github.com/shulhan/golangci-lint@v1.10.1/pkg/logutils/stderr_log.go (about)

     1  package logutils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/golangci/golangci-lint/pkg/exitcodes"
     8  	"github.com/sirupsen/logrus" //nolint:depguard
     9  )
    10  
    11  var isTestRun = os.Getenv("GL_TEST_RUN") == "1"
    12  
    13  type StderrLog struct {
    14  	name   string
    15  	logger *logrus.Logger
    16  	level  LogLevel
    17  }
    18  
    19  var _ Log = NewStderrLog("")
    20  
    21  func NewStderrLog(name string) *StderrLog {
    22  	sl := &StderrLog{
    23  		name:   name,
    24  		logger: logrus.New(),
    25  		level:  LogLevelWarn,
    26  	}
    27  
    28  	// control log level in logutils, not in logrus
    29  	sl.logger.SetLevel(logrus.DebugLevel)
    30  	sl.logger.Out = StdErr
    31  	sl.logger.Formatter = &logrus.TextFormatter{
    32  		DisableTimestamp: true, // `INFO[0007] msg` -> `INFO msg`
    33  	}
    34  	return sl
    35  }
    36  
    37  func exitIfTest() {
    38  	if isTestRun {
    39  		os.Exit(exitcodes.WarningInTest)
    40  	}
    41  }
    42  
    43  func (sl StderrLog) prefix() string {
    44  	prefix := ""
    45  	if sl.name != "" {
    46  		prefix = fmt.Sprintf("[%s] ", sl.name)
    47  	}
    48  
    49  	return prefix
    50  }
    51  
    52  func (sl StderrLog) Fatalf(format string, args ...interface{}) {
    53  	sl.logger.Errorf("%s%s", sl.prefix(), fmt.Sprintf(format, args...))
    54  	os.Exit(exitcodes.Failure)
    55  }
    56  
    57  func (sl StderrLog) Errorf(format string, args ...interface{}) {
    58  	if sl.level > LogLevelError {
    59  		return
    60  	}
    61  
    62  	sl.logger.Errorf("%s%s", sl.prefix(), fmt.Sprintf(format, args...))
    63  	// don't call exitIfTest() because the idea is to
    64  	// crash on hidden errors (warnings); but Errorf MUST NOT be
    65  	// called on hidden errors, see log levels comments.
    66  }
    67  
    68  func (sl StderrLog) Warnf(format string, args ...interface{}) {
    69  	if sl.level > LogLevelWarn {
    70  		return
    71  	}
    72  
    73  	sl.logger.Warnf("%s%s", sl.prefix(), fmt.Sprintf(format, args...))
    74  	exitIfTest()
    75  }
    76  
    77  func (sl StderrLog) Infof(format string, args ...interface{}) {
    78  	if sl.level > LogLevelInfo {
    79  		return
    80  	}
    81  
    82  	sl.logger.Infof("%s%s", sl.prefix(), fmt.Sprintf(format, args...))
    83  }
    84  
    85  func (sl StderrLog) Debugf(format string, args ...interface{}) {
    86  	if sl.level > LogLevelDebug {
    87  		return
    88  	}
    89  
    90  	sl.logger.Debugf("%s%s", sl.prefix(), fmt.Sprintf(format, args...))
    91  }
    92  
    93  func (sl StderrLog) Child(name string) Log {
    94  	prefix := ""
    95  	if sl.name != "" {
    96  		prefix = sl.name + "/"
    97  	}
    98  
    99  	child := sl
   100  	child.name = prefix + name
   101  
   102  	return &child
   103  }
   104  
   105  func (sl *StderrLog) SetLevel(level LogLevel) {
   106  	sl.level = level
   107  }