logger.go raw

   1  // Package log provides logging interface
   2  package log
   3  
   4  import (
   5  	"sync"
   6  )
   7  
   8  var (
   9  	protect sync.Once
  10  	log     *logControler
  11  )
  12  
  13  // Interface is the logging interface
  14  type Interface interface {
  15  	// Trace logs at trace level
  16  	// The attribute arguments are processed as follows:
  17  	// If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an key - value pair.
  18  	// Otherwise, the argument is treated as a value with key "!BADKEY".
  19  	Trace(msg string, args ...any)
  20  	// Debug logs at debug level,
  21  	// The attribute arguments are processed as follows:
  22  	// If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an key - value pair.
  23  	// Otherwise, the argument is treated as a value with key "!BADKEY".
  24  	Debug(msg string, args ...any)
  25  	// Info logs at info level
  26  	// The attribute arguments are processed as follows:
  27  	// If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an key - value pair.
  28  	// Otherwise, the argument is treated as a value with key "!BADKEY".
  29  	Info(msg string, args ...any)
  30  	// Warn logs at warn level
  31  	// The attribute arguments are processed as follows:
  32  	// If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an key - value pair.
  33  	// Otherwise, the argument is treated as a value with key "!BADKEY".
  34  	Warn(msg string, args ...any)
  35  	// Error logs at error level
  36  	// The attribute arguments are processed as follows:
  37  	// If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an key - value pair.
  38  	// Otherwise, the argument is treated as a value with key "!BADKEY".
  39  	Error(msg string, args ...any)
  40  	// Fatal logs at fatal level, followed by an exit.
  41  	// The attribute arguments are processed as follows:
  42  	// If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an key - value pair.
  43  	// Otherwise, the argument is treated as a value with key "!BADKEY".
  44  	Fatal(msg string, args ...any)
  45  
  46  	// Tracef formats according to a format specifier and logs the resulting string at trace level
  47  	Tracef(msg string, args ...any)
  48  	// Debugf formats according to a format specifier and logs the resulting string at debug level
  49  	Debugf(msg string, args ...any)
  50  	// Infof formats according to a format specifier and logs the resulting string at info level
  51  	Infof(msg string, args ...any)
  52  	// Warnf formats according to a format specifier and logs the resulting string at warn level
  53  	Warnf(msg string, args ...any)
  54  	// Errorf formats according to a format specifier and logs the resulting string at error level
  55  	Errorf(msg string, args ...any)
  56  	// Fatalf formats according to a format specifier and logs the resulting string at fatal level, followed by an exit.
  57  	Fatalf(msg string, args ...any)
  58  
  59  	// With returns new Interface that includes given fields with each operation
  60  	// returned logger will log each field on every logging operation it will be used
  61  	With(key string, fields Fields) Interface
  62  }
  63  
  64  // logControler is a wrapper for Interface
  65  type logControler struct {
  66  	log Interface
  67  }
  68  
  69  // SetLogger sets default logger interface
  70  func SetLogger(l Interface) {
  71  	log = &logControler{log: l}
  72  }
  73  
  74  // Default returns logger interface
  75  func Default() Interface {
  76  	protect.Do(func() {
  77  		if log == nil {
  78  			SetLogger(NewSlogAdapter(slogDefaultHandler))
  79  		}
  80  	})
  81  	return log.log
  82  }
  83  
  84  // NOPLogger returns discarding logger
  85  func NOPLogger() Interface {
  86  	return NewSlogAdapter(slogNOPHandler)
  87  }
  88