...

Source file src/github.com/growthbook/growthbook-golang/logging.go

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Logger is a common interface for logging information and warning
     8  // messages (errors are returned directly by SDK functions, but there
     9  // is some useful "out of band" data that's provided via this
    10  // interface).
    11  type Logger interface {
    12  	Error(msg string, args ...interface{})
    13  	Errorf(format string, args ...interface{})
    14  	Warn(msg string, args ...interface{})
    15  	Warnf(format string, args ...interface{})
    16  	Info(msg string, args ...interface{})
    17  	Infof(format string, args ...interface{})
    18  }
    19  
    20  // SetLogger sets up the logging interface used throughout. The idea
    21  // here is to provide developers with the option of handling errors
    22  // and warnings in a strict way during development and a lenient way
    23  // in production. For example, in development, setting a logger that
    24  // prints a message for all logged output and panics on any logged
    25  // warning or error might be appropriate, while in production, it
    26  // would make more sense to log only warnings and errors and to
    27  // proceed without halting. All GrowthBook SDK functions leave values
    28  // in a sensible default state after errors, so production systems can
    29  // essentially ignore any errors.
    30  func SetLogger(userLogger Logger) {
    31  	logger = userLogger
    32  }
    33  
    34  // Global private logging interface.
    35  var logger Logger
    36  
    37  // Internal logging functions wired up to logging interface.
    38  
    39  func logError(msg string, args ...interface{}) {
    40  	if logger != nil {
    41  		logger.Error(msg, args...)
    42  	}
    43  }
    44  
    45  func logErrorf(format string, args ...interface{}) {
    46  	if logger != nil {
    47  		logger.Errorf(format, args...)
    48  	}
    49  }
    50  
    51  func logWarn(msg string, args ...interface{}) {
    52  	if logger != nil {
    53  		logger.Warn(msg, args...)
    54  	}
    55  }
    56  
    57  func logWarnf(format string, args ...interface{}) {
    58  	if logger != nil {
    59  		logger.Warnf(format, args...)
    60  	}
    61  }
    62  
    63  func logInfo(msg string, args ...interface{}) {
    64  	if logger != nil {
    65  		logger.Info(msg, args...)
    66  	}
    67  }
    68  
    69  func logInfof(format string, args ...interface{}) {
    70  	if logger != nil {
    71  		logger.Infof(format, args...)
    72  	}
    73  }
    74  
    75  // DevLogger is a logger instance suitable for use in development. It
    76  // prints all logged messages to standard output, and exits on errors.
    77  type DevLogger struct{}
    78  
    79  func (log *DevLogger) Error(msg string, args ...interface{}) {
    80  	fmt.Print("[ERROR] ", msg)
    81  	if len(args) > 0 {
    82  		fmt.Print(": ")
    83  		fmt.Println(args...)
    84  	}
    85  }
    86  
    87  func (log *DevLogger) Errorf(format string, args ...interface{}) {
    88  	fmt.Printf("[ERROR] "+format+"\n", args...)
    89  }
    90  
    91  func (log *DevLogger) Warn(msg string, args ...interface{}) {
    92  	fmt.Print("[WARNING] ", msg)
    93  	if len(args) > 0 {
    94  		fmt.Print(": ")
    95  		fmt.Println(args...)
    96  	}
    97  }
    98  
    99  func (log *DevLogger) Warnf(format string, args ...interface{}) {
   100  	fmt.Printf("[WARNING] "+format+"\n", args...)
   101  }
   102  
   103  func (log *DevLogger) Info(msg string, args ...interface{}) {
   104  	fmt.Print("[INFO] ", msg)
   105  	if len(args) > 0 {
   106  		fmt.Print(": ")
   107  		fmt.Println(args...)
   108  	}
   109  }
   110  
   111  func (log *DevLogger) Infof(format string, args ...interface{}) {
   112  	fmt.Printf("[INFO] "+format+"\n", args...)
   113  }
   114  

View as plain text