github.com/daixiang0/gci@v0.13.0/pkg/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"sync"
     5  
     6  	"go.uber.org/zap"
     7  	"go.uber.org/zap/zapcore"
     8  )
     9  
    10  // Use L to log with Zap
    11  var logger *zap.Logger
    12  
    13  // Keep the config to reference the atomicLevel for changing levels
    14  var logConfig zap.Config
    15  
    16  var doOnce sync.Once
    17  
    18  // InitLogger sets up the logger
    19  func InitLogger() {
    20  	doOnce.Do(func() {
    21  		logConfig = zap.NewDevelopmentConfig()
    22  
    23  		logConfig.EncoderConfig.TimeKey = "timestamp"
    24  		logConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
    25  		logConfig.Level.SetLevel(zapcore.InfoLevel)
    26  		logConfig.OutputPaths = []string{"stderr"}
    27  
    28  		var err error
    29  		logger, err = logConfig.Build()
    30  		if err != nil {
    31  			panic(err)
    32  		}
    33  	})
    34  }
    35  
    36  // SetLevel allows you to set the level of the default gci logger.
    37  // This will not work if you replace the logger
    38  func SetLevel(level zapcore.Level) {
    39  	logConfig.Level.SetLevel(level)
    40  }
    41  
    42  // L returns the logger
    43  func L() *zap.Logger {
    44  	return logger
    45  }
    46  
    47  // SetLogger allows you to set the logger to whatever you want
    48  func SetLogger(l *zap.Logger) {
    49  	logger = l
    50  }