github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/internal/log/log.go (about)

     1  /*
     2  Package log contains the singleton object and helper functions for facilitating logging within the syft library.
     3  */
     4  package log
     5  
     6  import (
     7  	"github.com/anchore/go-logger"
     8  	"github.com/anchore/go-logger/adapter/discard"
     9  	"github.com/anchore/go-logger/adapter/redact"
    10  	red "github.com/anchore/syft/internal/redact"
    11  )
    12  
    13  // log is the singleton used to facilitate logging internally within
    14  var log = discard.New()
    15  
    16  func Set(l logger.Logger) {
    17  	// though the application will automatically have a redaction logger, library consumers may not be doing this.
    18  	// for this reason we additionally ensure there is a redaction logger configured for any logger passed. The
    19  	// source of truth for redaction values is still in the internal redact package. If the passed logger is already
    20  	// redacted, then this is a no-op.
    21  	store := red.Get()
    22  	if store != nil {
    23  		l = redact.New(l, store)
    24  	}
    25  	log = l
    26  }
    27  
    28  func Get() logger.Logger {
    29  	return log
    30  }
    31  
    32  // Errorf takes a formatted template string and template arguments for the error logging level.
    33  func Errorf(format string, args ...interface{}) {
    34  	log.Errorf(format, args...)
    35  }
    36  
    37  // Error logs the given arguments at the error logging level.
    38  func Error(args ...interface{}) {
    39  	log.Error(args...)
    40  }
    41  
    42  // Warnf takes a formatted template string and template arguments for the warning logging level.
    43  func Warnf(format string, args ...interface{}) {
    44  	log.Warnf(format, args...)
    45  }
    46  
    47  // Warn logs the given arguments at the warning logging level.
    48  func Warn(args ...interface{}) {
    49  	log.Warn(args...)
    50  }
    51  
    52  // Infof takes a formatted template string and template arguments for the info logging level.
    53  func Infof(format string, args ...interface{}) {
    54  	log.Infof(format, args...)
    55  }
    56  
    57  // Info logs the given arguments at the info logging level.
    58  func Info(args ...interface{}) {
    59  	log.Info(args...)
    60  }
    61  
    62  // Debugf takes a formatted template string and template arguments for the debug logging level.
    63  func Debugf(format string, args ...interface{}) {
    64  	log.Debugf(format, args...)
    65  }
    66  
    67  // Debug logs the given arguments at the debug logging level.
    68  func Debug(args ...interface{}) {
    69  	log.Debug(args...)
    70  }
    71  
    72  // Tracef takes a formatted template string and template arguments for the trace logging level.
    73  func Tracef(format string, args ...interface{}) {
    74  	log.Tracef(format, args...)
    75  }
    76  
    77  // Trace logs the given arguments at the trace logging level.
    78  func Trace(args ...interface{}) {
    79  	log.Trace(args...)
    80  }
    81  
    82  // WithFields returns a message logger with multiple key-value fields.
    83  func WithFields(fields ...interface{}) logger.MessageLogger {
    84  	return log.WithFields(fields...)
    85  }
    86  
    87  // Nested returns a new logger with hard coded key-value pairs
    88  func Nested(fields ...interface{}) logger.Logger {
    89  	return log.Nested(fields...)
    90  }