amuz.es/src/infra/goutils@v0.1.3/logger/zap/logger.go (about)

     1  package zap
     2  
     3  import (
     4  	"go.uber.org/zap"
     5  
     6  	"go.uber.org/zap/zapcore"
     7  	"amuz.es/src/infra/goutils/logger"
     8  	"amuz.es/src/infra/goutils/logger/rotater"
     9  )
    10  
    11  var (
    12  	defaultWriter             logger.RotateSyncer
    13  	defaultErrorOutputOptions []zap.Option
    14  	nopCloser                 = func() (err error) { return }
    15  )
    16  
    17  func init() {
    18  	zap.RedirectStdLog(zap.L())
    19  }
    20  
    21  func replaceGlobalLogger(newOne *zap.Logger) {
    22  	zap.ReplaceGlobals(newOne)
    23  	zap.RedirectStdLog(newOne)
    24  }
    25  
    26  func Init(
    27  	verbose bool,
    28  	formatter zapcore.Encoder,
    29  	mainLogName, logFilename, logDir string,
    30  	rotateOption []rotater.Option,
    31  	logLevel zapcore.Level,
    32  	additionalOptions ...zap.Option,
    33  ) (*zap.SugaredLogger, error) {
    34  	level := zap.NewAtomicLevelAt(logLevel)
    35  	if defaultWriter, err := rotater.NewLogWriter(logFilename, logDir, rotateOption...); err != nil {
    36  		return nil, err
    37  	} else {
    38  		defaultErrorOutputOptions = []zap.Option{zap.ErrorOutput(defaultWriter)}
    39  		options := defaultErrorOutputOptions
    40  		if verbose {
    41  			options = append(options, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.PanicLevel)))
    42  		}
    43  		// reset log option slice
    44  		options = append(options, additionalOptions...)
    45  
    46  		log := initLogger(defaultWriter, mainLogName, formatter, level, options...)
    47  
    48  		replaceGlobalLogger(log)
    49  		return log.Sugar(), nil
    50  	}
    51  }
    52  
    53  func New(parent *zap.SugaredLogger, moduleName string, options ...zap.Option) *zap.SugaredLogger {
    54  	var subLogger *zap.Logger
    55  	if parent == nil {
    56  		subLogger = zap.L().Named(moduleName)
    57  	} else {
    58  		subLogger = parent.Desugar().Named(moduleName)
    59  	}
    60  
    61  	subLogger.WithOptions(options...)
    62  
    63  	return subLogger.Sugar()
    64  }
    65  
    66  func NewOtherLogger(
    67  	formatter zapcore.Encoder,
    68  	moduleName, logFilename, logDir string,
    69  	rotateOption []rotater.Option,
    70  	logLevel zapcore.Level,
    71  	fields ...zapcore.Field,
    72  ) (logger *zap.SugaredLogger, closer func() error, err error) {
    73  	loglevel := zap.NewAtomicLevelAt(logLevel)
    74  	logWriter, err := rotater.NewLogWriter(logFilename, logDir, rotateOption...)
    75  	if err != nil {
    76  		return
    77  	}
    78  	core := zapcore.NewCore(formatter, logWriter, loglevel)
    79  	closer = logWriter.Close
    80  	logger = zap.New(core, defaultErrorOutputOptions...).
    81  		Named(moduleName).With(fields...).Sugar()
    82  	return
    83  }
    84  
    85  func NewOtherLoggerWithOption(
    86  	formatter zapcore.Encoder,
    87  	moduleName, logFilename, logDir string,
    88  	rotateOption []rotater.Option,
    89  	logLevel zapcore.Level,
    90  	options []zap.Option,
    91  	fields ...zapcore.Field,
    92  ) (logger *zap.SugaredLogger, closer func() error, err error) {
    93  	loglevel := zap.NewAtomicLevelAt(logLevel)
    94  	logWriter, err := rotater.NewLogWriter(logFilename, logDir, rotateOption...)
    95  	if err != nil {
    96  		return
    97  	}
    98  	core := zapcore.NewCore(formatter, logWriter, loglevel)
    99  	closer = logWriter.Close
   100  	options = append(defaultErrorOutputOptions, options...)
   101  	logger = zap.New(core, options...).
   102  		Named(moduleName).With(fields...).Sugar()
   103  	return
   104  }
   105  func initLogger(
   106  	writer zapcore.WriteSyncer,
   107  	moduleName string,
   108  	formatter zapcore.Encoder,
   109  	level zap.AtomicLevel,
   110  	options ...zap.Option,
   111  ) *zap.Logger {
   112  	core := zapcore.NewCore(formatter, writer, level)
   113  	return zap.New(core, options...).Named(moduleName)
   114  }