github.com/jxskiss/gopkg@v0.17.3/zlog/logger.go (about)

     1  package zlog
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"go.uber.org/zap"
     8  
     9  	"github.com/jxskiss/gopkg/internal/linkname"
    10  )
    11  
    12  var _ Logger = (*zap.SugaredLogger)(nil)
    13  
    14  // Logger is a generic logger interface that output logs with a format.
    15  // It's implemented by many logging libraries, including logrus.Logger,
    16  // zap.SugaredLogger, etc.
    17  //
    18  // Within this package, StdLogger is a default implementation which sends
    19  // log messages to the standard library, it also adds the level prefix to
    20  // the output message.
    21  type Logger interface {
    22  	Debugf(format string, args ...interface{})
    23  	Infof(format string, args ...interface{})
    24  	Warnf(format string, args ...interface{})
    25  	Errorf(format string, args ...interface{})
    26  	Fatalf(format string, args ...interface{})
    27  }
    28  
    29  // -------- standard library logger -------- //
    30  
    31  // StdLogger is a default implementation of Logger which sends log messages
    32  // to the standard library.
    33  //
    34  // It follows the global logging level of this package, the level can be
    35  // changed by calling SetLevel.
    36  var StdLogger Logger = stdLogger{}
    37  
    38  type stdLogger struct{}
    39  
    40  // log_std links to log.std to get correct caller depth for both
    41  // with and without setting GlobalConfig.RedirectStdLog.
    42  var log_std = linkname.LogStd
    43  
    44  const _stdLogDepth = 2
    45  
    46  func (_ stdLogger) Debugf(format string, args ...interface{}) {
    47  	if GetLevel() <= DebugLevel {
    48  		log_std.Output(_stdLogDepth, fmt.Sprintf(DebugPrefix+format, args...))
    49  	}
    50  }
    51  
    52  func (_ stdLogger) Infof(format string, args ...interface{}) {
    53  	if GetLevel() <= InfoLevel {
    54  		log_std.Output(_stdLogDepth, fmt.Sprintf(InfoPrefix+format, args...))
    55  	}
    56  }
    57  
    58  func (_ stdLogger) Warnf(format string, args ...interface{}) {
    59  	if GetLevel() <= WarnLevel {
    60  		log_std.Output(_stdLogDepth, fmt.Sprintf(WarnPrefix+format, args...))
    61  	}
    62  }
    63  
    64  func (_ stdLogger) Errorf(format string, args ...interface{}) {
    65  	if GetLevel() <= ErrorLevel {
    66  		log_std.Output(_stdLogDepth, fmt.Sprintf(ErrorPrefix+format, args...))
    67  	}
    68  }
    69  
    70  func (_ stdLogger) Fatalf(format string, args ...interface{}) {
    71  	log_std.Output(_stdLogDepth, fmt.Sprintf(FatalPrefix+format, args...))
    72  	Sync()
    73  	os.Exit(1)
    74  }
    75  
    76  // -------- nop logger -------- //
    77  
    78  // NopLogger is a logger which discards anything it receives.
    79  var NopLogger Logger = &nopLogger{}
    80  
    81  type nopLogger struct{}
    82  
    83  func (_ nopLogger) Debugf(format string, args ...interface{}) {}
    84  
    85  func (_ nopLogger) Infof(format string, args ...interface{}) {}
    86  
    87  func (_ nopLogger) Warnf(format string, args ...interface{}) {}
    88  
    89  func (_ nopLogger) Errorf(format string, args ...interface{}) {}
    90  
    91  func (_ nopLogger) Fatalf(format string, args ...interface{}) {
    92  	Sync()
    93  	os.Exit(1)
    94  }