github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/base/logger.go (about)

     1  package base
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"time"
     8  )
     9  
    10  const logTagFmt = "%s %s"
    11  
    12  // Logger defines an interface for writing log messages.
    13  type Logger interface {
    14  	Info(args ...interface{})
    15  	Warn(args ...interface{})
    16  	Error(args ...interface{})
    17  	Cost(arg ...interface{}) func()
    18  	Infof(format string, args ...interface{})
    19  	Warnf(format string, args ...interface{})
    20  	Errorf(format string, args ...interface{})
    21  	Fatalf(format string, args ...interface{})
    22  }
    23  
    24  func NewLogger(logger Logger, tag string) Logger {
    25  	if logger == nil {
    26  		return defaultLogger{
    27  			tag: tag,
    28  		}
    29  	} else {
    30  		return customLogger{
    31  			clog: logger,
    32  			tag:  tag,
    33  		}
    34  	}
    35  }
    36  
    37  type customLogger struct {
    38  	clog Logger
    39  	tag  string
    40  }
    41  
    42  func (l customLogger) Info(args ...interface{}) {
    43  	l.clog.Info(l.tag, " ", fmt.Sprint(args...))
    44  }
    45  
    46  func (l customLogger) Warn(args ...interface{}) {
    47  	l.clog.Warn(l.tag, " ", fmt.Sprint(args...))
    48  }
    49  
    50  func (l customLogger) Error(args ...interface{}) {
    51  	l.clog.Error(l.tag, " ", fmt.Sprint(args...))
    52  }
    53  
    54  func (l customLogger) Infof(format string, args ...interface{}) {
    55  	l.clog.Infof(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    56  }
    57  
    58  func (l customLogger) Warnf(format string, args ...interface{}) {
    59  	l.clog.Warnf(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    60  }
    61  
    62  func (l customLogger) Errorf(format string, args ...interface{}) {
    63  	l.clog.Errorf(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    64  }
    65  
    66  func (l customLogger) Fatalf(format string, args ...interface{}) {
    67  	l.clog.Fatalf(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    68  }
    69  
    70  func (l customLogger) Cost(args ...interface{}) func() {
    71  	return l.clog.Cost(l.tag, " ", fmt.Sprint(args...))
    72  }
    73  
    74  // DefaultLogger logs to the Go stdlib logs.
    75  type defaultLogger struct {
    76  	tag string
    77  }
    78  
    79  var DefaultLogger = defaultLogger{tag: ""}
    80  
    81  func (l defaultLogger) Info(args ...interface{}) {
    82  	_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...)))
    83  }
    84  
    85  func (l defaultLogger) Warn(args ...interface{}) {
    86  	_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...)))
    87  }
    88  
    89  func (l defaultLogger) Error(args ...interface{}) {
    90  	_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...)))
    91  }
    92  
    93  func (l defaultLogger) Infof(format string, args ...interface{}) {
    94  	_ = log.Output(2, fmt.Sprintf(logTagFmt, l.tag, fmt.Sprintf(format, args...)))
    95  }
    96  
    97  func (l defaultLogger) Warnf(format string, args ...interface{}) {
    98  	_ = log.Output(2, fmt.Sprintf(logTagFmt, l.tag, fmt.Sprintf(format, args...)))
    99  }
   100  
   101  func (l defaultLogger) Errorf(format string, args ...interface{}) {
   102  	_ = log.Output(2, fmt.Sprintf(logTagFmt, l.tag, fmt.Sprintf(format, args...)))
   103  }
   104  
   105  func (l defaultLogger) Fatalf(format string, args ...interface{}) {
   106  	_ = log.Output(2, fmt.Sprintf(logTagFmt+format, l.tag, fmt.Sprint(args...)))
   107  	os.Exit(1)
   108  }
   109  
   110  func (l defaultLogger) Cost(args ...interface{}) func() {
   111  	begin := time.Now()
   112  	return func() {
   113  		_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...), " ", FmtDuration(time.Now().Sub(begin))))
   114  	}
   115  }
   116  
   117  func FmtDuration(d time.Duration) string {
   118  	if d > time.Second {
   119  		return fmt.Sprintf("cost:%d.%03ds", d/time.Second, d/time.Millisecond%1000)
   120  	}
   121  	if d > time.Millisecond {
   122  		return fmt.Sprintf("cost:%d.%03dms", d/time.Millisecond, d/time.Microsecond%1000)
   123  	}
   124  	if d > time.Microsecond {
   125  		return fmt.Sprintf("cost:%d.%03dus", d/time.Microsecond, d%1000)
   126  	}
   127  	return fmt.Sprintf("cost:%dns", d)
   128  }