github.com/keakon/golog@v0.0.0-20230330091222-cac71197c18d/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"github.com/keakon/golog"
     5  )
     6  
     7  var defaultLogger *golog.Logger
     8  
     9  // SetLogFunc set the log function with specified level for the defaultLogger.
    10  // This function should be called before SetDefaultLogger.
    11  func SetLogFunc(f func(args ...interface{}), level golog.Level) {
    12  	switch level {
    13  	case golog.DebugLevel:
    14  		Debug = f
    15  	case golog.InfoLevel:
    16  		Info = f
    17  	case golog.WarnLevel:
    18  		Warn = f
    19  	case golog.ErrorLevel:
    20  		Error = f
    21  	case golog.CritLevel:
    22  		Crit = f
    23  	}
    24  }
    25  
    26  // SetLogfFunc set the logf function with specified level for the defaultLogger.
    27  // This function should be called before SetDefaultLogger.
    28  func SetLogfFunc(f func(msg string, args ...interface{}), level golog.Level) {
    29  	switch level {
    30  	case golog.DebugLevel:
    31  		Debugf = f
    32  	case golog.InfoLevel:
    33  		Infof = f
    34  	case golog.WarnLevel:
    35  		Warnf = f
    36  	case golog.ErrorLevel:
    37  		Errorf = f
    38  	case golog.CritLevel:
    39  		Critf = f
    40  	}
    41  }
    42  
    43  // SetDefaultLogger set the logger as the defaultLogger.
    44  // The logging functions in this package use it as their logger.
    45  // This function should be called before using below functions.
    46  func SetDefaultLogger(l *golog.Logger) {
    47  	defaultLogger = l
    48  	if l == nil {
    49  		return
    50  	}
    51  	minLevel := l.GetMinLevel()
    52  	for level := golog.DebugLevel; level <= golog.CritLevel; level++ {
    53  		if level < minLevel {
    54  			switch level {
    55  			case golog.DebugLevel:
    56  				Debug = nop
    57  				Debugf = nopf
    58  			case golog.InfoLevel:
    59  				Info = nop
    60  				Infof = nopf
    61  			case golog.WarnLevel:
    62  				Warn = nop
    63  				Warnf = nopf
    64  			case golog.ErrorLevel:
    65  				Error = nop
    66  				Errorf = nopf
    67  			case golog.CritLevel:
    68  				Crit = nop
    69  				Critf = nopf
    70  			}
    71  		} else {
    72  			switch level {
    73  			case golog.DebugLevel:
    74  				Debug = logFuncs[level]
    75  				Debugf = logfFuncs[level]
    76  			case golog.InfoLevel:
    77  				Info = logFuncs[level]
    78  				Infof = logfFuncs[level]
    79  			case golog.WarnLevel:
    80  				Warn = logFuncs[level]
    81  				Warnf = logfFuncs[level]
    82  			case golog.ErrorLevel:
    83  				Error = logFuncs[level]
    84  				Errorf = logfFuncs[level]
    85  			case golog.CritLevel:
    86  				Crit = logFuncs[level]
    87  				Critf = logfFuncs[level]
    88  			}
    89  		}
    90  	}
    91  }
    92  
    93  // IsEnabledFor returns whether the default logger is enabled for the level.
    94  func IsEnabledFor(level golog.Level) bool {
    95  	return defaultLogger != nil && defaultLogger.IsEnabledFor(level)
    96  }
    97  
    98  func nop(args ...interface{})              {}
    99  func nopf(msg string, args ...interface{}) {}
   100  
   101  var (
   102  	// Debug logs a _debug level message. It uses fmt.Fprint() to format args.
   103  	Debug = nop
   104  	// Info logs a _info level message. It uses fmt.Fprint() to format args.
   105  	Info = nop
   106  	// Warn logs a _warning level message. It uses fmt.Fprint() to format args.
   107  	Warn = nop
   108  	// Error logs an _error level message. It uses fmt.Fprint() to format args.
   109  	Error = nop
   110  	// Crit logs a _critical level message. It uses fmt.Fprint() to format args.
   111  	Crit = nop
   112  
   113  	// Debugf logs a _debug level message. It uses fmt.Fprintf() to format msg and args.
   114  	Debugf = nopf
   115  	// Infof logs a _info level message. It uses fmt.Fprintf() to format msg and args.
   116  	Infof = nopf
   117  	// Warnf logs a _warning level message. It uses fmt.Fprintf() to format msg and args.
   118  	Warnf = nopf
   119  	// Errorf logs a _error level message. It uses fmt.Fprintf() to format msg and args.
   120  	Errorf = nopf
   121  	// Critf logs a _critical level message. It uses fmt.Fprintf() to format msg and args.
   122  	Critf = nopf
   123  
   124  	logFuncs = []func(args ...interface{}){
   125  		func(args ...interface{}) {
   126  			file, line := golog.Caller(1) // deeper caller will be more expensive, so don't use init() to initialize those functions
   127  			defaultLogger.Log(golog.DebugLevel, file, line, "", args...)
   128  		},
   129  		func(args ...interface{}) {
   130  			file, line := golog.Caller(1)
   131  			defaultLogger.Log(golog.InfoLevel, file, line, "", args...)
   132  		},
   133  		func(args ...interface{}) {
   134  			file, line := golog.Caller(1)
   135  			defaultLogger.Log(golog.WarnLevel, file, line, "", args...)
   136  		},
   137  		func(args ...interface{}) {
   138  			file, line := golog.Caller(1)
   139  			defaultLogger.Log(golog.ErrorLevel, file, line, "", args...)
   140  		},
   141  		func(args ...interface{}) {
   142  			file, line := golog.Caller(1)
   143  			defaultLogger.Log(golog.CritLevel, file, line, "", args...)
   144  		},
   145  	}
   146  
   147  	logfFuncs = []func(msg string, args ...interface{}){
   148  		func(msg string, args ...interface{}) {
   149  			file, line := golog.Caller(1)
   150  			defaultLogger.Log(golog.DebugLevel, file, line, msg, args...)
   151  		},
   152  		func(msg string, args ...interface{}) {
   153  			file, line := golog.Caller(1)
   154  			defaultLogger.Log(golog.InfoLevel, file, line, msg, args...)
   155  		},
   156  		func(msg string, args ...interface{}) {
   157  			file, line := golog.Caller(1)
   158  			defaultLogger.Log(golog.WarnLevel, file, line, msg, args...)
   159  		},
   160  		func(msg string, args ...interface{}) {
   161  			file, line := golog.Caller(1)
   162  			defaultLogger.Log(golog.ErrorLevel, file, line, msg, args...)
   163  		},
   164  		func(msg string, args ...interface{}) {
   165  			file, line := golog.Caller(1)
   166  			defaultLogger.Log(golog.CritLevel, file, line, msg, args...)
   167  		},
   168  	}
   169  )