github.com/avicd/go-utilx@v0.1.0/logx/logger.go (about)

     1  package logx
     2  
     3  import (
     4  	"github.com/avicd/go-utilx/conv"
     5  	"time"
     6  )
     7  
     8  type Level = int
     9  
    10  const (
    11  	ALL Level = iota
    12  	DEBUG
    13  	INFO
    14  	WARN
    15  	ERROR
    16  	FATAL
    17  	OFF
    18  )
    19  
    20  type CKind = uint
    21  
    22  const (
    23  	CLabel CKind = iota
    24  	CMsg
    25  	CLabelMsg
    26  	CAll
    27  )
    28  
    29  var Labels = map[Level]string{
    30  	DEBUG: "DEBUG",
    31  	INFO:  "INFO",
    32  	WARN:  "WARN",
    33  	ERROR: "ERROR",
    34  	FATAL: "FATAL",
    35  }
    36  
    37  type Logger interface {
    38  	SetLevel(level Level)
    39  	GetLevel() Level
    40  	Debug(args ...any)
    41  	Debugf(format string, args ...any)
    42  	Info(args ...any)
    43  	Infof(format string, args ...any)
    44  	Warn(args ...any)
    45  	Warnf(format string, args ...any)
    46  	Error(args ...any)
    47  	Errorf(format string, args ...any)
    48  	Fatal(args ...any)
    49  	Fatalf(format string, args ...any)
    50  }
    51  
    52  type Appender interface {
    53  	Write(level Level, args ...any)
    54  }
    55  
    56  var logger Logger
    57  
    58  func init() {
    59  	logger = LoggerOf(&ConsoleAppender{})
    60  	logger.SetLevel(ALL)
    61  }
    62  
    63  func Default() Logger {
    64  	return logger
    65  }
    66  
    67  func SetDefault(nlog Logger) {
    68  	if nlog != nil {
    69  		logger = nlog
    70  	}
    71  }
    72  
    73  func LoggerOf(appender Appender) Logger {
    74  	return &Proxy{
    75  		Appender: appender,
    76  	}
    77  }
    78  
    79  func timeNow(layout string) string {
    80  	if layout == "" {
    81  		layout = conv.DateTimeMirco
    82  	}
    83  	return time.Now().Format(layout) + " "
    84  }
    85  
    86  func Debug(args ...any) {
    87  	Default().Debug(args...)
    88  }
    89  
    90  func Debugf(format string, args ...any) {
    91  	Default().Debugf(format, args...)
    92  }
    93  
    94  func Info(args ...any) {
    95  	Default().Info(args...)
    96  }
    97  func Infof(format string, args ...any) {
    98  	Default().Infof(format, args...)
    99  }
   100  
   101  func Warn(args ...any) {
   102  	Default().Warn(args...)
   103  }
   104  
   105  func Warnf(format string, args ...any) {
   106  	Default().Warnf(format, args...)
   107  }
   108  
   109  func Error(args ...any) {
   110  	Default().Error(args...)
   111  }
   112  
   113  func Errorf(format string, args ...any) {
   114  	Default().Errorf(format, args...)
   115  }
   116  
   117  func Fatal(args ...any) {
   118  	Default().Fatal(args...)
   119  }
   120  
   121  func Fatalf(format string, args ...any) {
   122  	Default().Fatalf(format, args...)
   123  }