bitbucket.org/ai69/amoy@v0.2.3/logger_set.go (about)

     1  package amoy
     2  
     3  import (
     4  	"os"
     5  
     6  	"go.uber.org/zap"
     7  	"go.uber.org/zap/zapcore"
     8  )
     9  
    10  // NewJSONLogger returns a Logger with given log path and debug mode and all output format is JSON.
    11  func NewJSONLogger(fileName string, debug bool) *Logger {
    12  	return NewLogger(fileName, debug, LogConfig{
    13  		ConsoleFormat: "json",
    14  		FileFormat:    "json",
    15  		MaxFileSizeMB: 10,
    16  		MaxBackups:    10,
    17  		CompressFile:  true,
    18  	})
    19  }
    20  
    21  // NewPersistentLogger returns a Logger with given log path and debug mode and output in console with color, save as json, and never cleans up.
    22  func NewPersistentLogger(fileName string, debug bool) *Logger {
    23  	return NewLogger(fileName, debug, LogConfig{
    24  		ConsoleFormat: "console",
    25  		FileFormat:    "json",
    26  		MaxFileSizeMB: 10,
    27  		CompressFile:  true,
    28  	})
    29  }
    30  
    31  // NewMonochromeLogger returns a Logger with given log path and debug mode and output in console, save as json, and never cleans up.
    32  func NewMonochromeLogger(fileName string, debug bool) *Logger {
    33  	return NewLogger(fileName, debug, LogConfig{
    34  		ConsoleFormat: "console",
    35  		FileFormat:    "json",
    36  		Monochrome:    true,
    37  		MaxFileSizeMB: 10,
    38  		CompressFile:  true,
    39  	})
    40  }
    41  
    42  // NoopZapLogger returns a zap logger enabled at fatal level, it basically logs nothing.
    43  func NoopZapLogger() *zap.Logger {
    44  	logger, err := zap.Config{
    45  		Level:             zap.NewAtomicLevelAt(zap.FatalLevel),
    46  		Development:       false,
    47  		DisableCaller:     true,
    48  		DisableStacktrace: true,
    49  		Encoding:          "console",
    50  		EncoderConfig:     zapcore.EncoderConfig{},
    51  	}.Build()
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	return logger
    56  }
    57  
    58  // SimpleZapLogger returns a zap logger with color console as output.
    59  func SimpleZapLogger() *zap.Logger {
    60  	logger := NewPersistentLogger("", true)
    61  	return logger.Logger().With(zap.Int("pid", os.Getpid()))
    62  }
    63  
    64  // SimpleZapSugaredLogger returns a sugared zap logger with color console as output.
    65  func SimpleZapSugaredLogger() *zap.SugaredLogger {
    66  	logger := NewPersistentLogger("", true)
    67  	return logger.LoggerSugared().With(zap.Int("pid", os.Getpid()))
    68  }