github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/base/logger.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package base
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  	"os"
    21  	"time"
    22  )
    23  
    24  const logTagFmt = "%s %s"
    25  
    26  type Logger interface {
    27  	Info(args ...interface{})
    28  	Warn(args ...interface{})
    29  	Error(args ...interface{})
    30  	Cost(arg ...interface{}) func()
    31  	Infof(format string, args ...interface{})
    32  	Warnf(format string, args ...interface{})
    33  	Errorf(format string, args ...interface{})
    34  	Fatalf(format string, args ...interface{})
    35  }
    36  
    37  func NewLogger(logger Logger, tag string) Logger {
    38  	if logger == nil {
    39  		return defaultLogger{
    40  			tag: tag,
    41  		}
    42  	} else {
    43  		return customLogger{
    44  			clog: logger,
    45  			tag:  tag,
    46  		}
    47  	}
    48  }
    49  
    50  type customLogger struct {
    51  	clog Logger
    52  	tag  string
    53  }
    54  
    55  func (l customLogger) Info(args ...interface{}) {
    56  	l.clog.Info(l.tag, " ", fmt.Sprint(args...))
    57  }
    58  
    59  func (l customLogger) Warn(args ...interface{}) {
    60  	l.clog.Warn(l.tag, " ", fmt.Sprint(args...))
    61  }
    62  
    63  func (l customLogger) Error(args ...interface{}) {
    64  	l.clog.Error(l.tag, " ", fmt.Sprint(args...))
    65  }
    66  
    67  func (l customLogger) Infof(format string, args ...interface{}) {
    68  	l.clog.Infof(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    69  }
    70  
    71  func (l customLogger) Warnf(format string, args ...interface{}) {
    72  	l.clog.Warnf(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    73  }
    74  
    75  func (l customLogger) Errorf(format string, args ...interface{}) {
    76  	l.clog.Errorf(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    77  }
    78  
    79  func (l customLogger) Fatalf(format string, args ...interface{}) {
    80  	l.clog.Fatalf(logTagFmt, l.tag, fmt.Sprintf(format, args...))
    81  }
    82  
    83  func (l customLogger) Cost(args ...interface{}) func() {
    84  	return l.clog.Cost(l.tag, " ", fmt.Sprint(args...))
    85  }
    86  
    87  type defaultLogger struct {
    88  	tag string
    89  }
    90  
    91  var DefaultLogger = defaultLogger{tag: ""}
    92  
    93  func (l defaultLogger) Info(args ...interface{}) {
    94  	_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...)))
    95  }
    96  
    97  func (l defaultLogger) Warn(args ...interface{}) {
    98  	_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...)))
    99  }
   100  
   101  func (l defaultLogger) Error(args ...interface{}) {
   102  	_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...)))
   103  }
   104  
   105  func (l defaultLogger) Infof(format string, args ...interface{}) {
   106  	_ = log.Output(2, fmt.Sprintf(logTagFmt, l.tag, fmt.Sprintf(format, args...)))
   107  }
   108  
   109  func (l defaultLogger) Warnf(format string, args ...interface{}) {
   110  	_ = log.Output(2, fmt.Sprintf(logTagFmt, l.tag, fmt.Sprintf(format, args...)))
   111  }
   112  
   113  func (l defaultLogger) Errorf(format string, args ...interface{}) {
   114  	_ = log.Output(2, fmt.Sprintf(logTagFmt, l.tag, fmt.Sprintf(format, args...)))
   115  }
   116  
   117  func (l defaultLogger) Fatalf(format string, args ...interface{}) {
   118  	_ = log.Output(2, fmt.Sprintf(logTagFmt+format, l.tag, fmt.Sprint(args...)))
   119  	os.Exit(1)
   120  }
   121  
   122  func (l defaultLogger) Cost(args ...interface{}) func() {
   123  	begin := time.Now()
   124  	return func() {
   125  		_ = log.Output(2, fmt.Sprint(l.tag, " ", fmt.Sprint(args...), " ", FmtDuration(time.Since(begin))))
   126  	}
   127  }
   128  
   129  func FmtDuration(d time.Duration) string {
   130  	if d > time.Second {
   131  		return fmt.Sprintf("cost:%d.%03ds", d/time.Second, d/time.Millisecond%1000)
   132  	}
   133  	if d > time.Millisecond {
   134  		return fmt.Sprintf("cost:%d.%03dms", d/time.Millisecond, d/time.Microsecond%1000)
   135  	}
   136  	if d > time.Microsecond {
   137  		return fmt.Sprintf("cost:%d.%03dus", d/time.Microsecond, d%1000)
   138  	}
   139  	return fmt.Sprintf("cost:%dns", d)
   140  }