1
0
Fork 0

added option prameter

This commit is contained in:
Sangbum Kim 2018-01-16 15:08:24 +09:00
parent 437417de21
commit 0a9f4c0597
1 changed files with 37 additions and 23 deletions

View File

@ -9,9 +9,9 @@ import (
)
var (
defaultOptions []zap.Option
defaultWriter logger.RotateSyncer
nopCloser = func() (err error) { return }
defaultWriter logger.RotateSyncer
defaultErrorOutputOptions []zap.Option
nopCloser = func() (err error) { return }
)
func init() {
@ -29,28 +29,36 @@ func Init(
mainLogName, logFilename, logDir string,
maxSizeMb, maxBackup, maxDay int,
logLevel zapcore.Level,
additionalOptions ...zap.Option,
) *zap.SugaredLogger {
level := zap.NewAtomicLevelAt(logLevel)
defaultWriter = rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
//encoderOption := getEncoderOption(verbose)
//formatter := zapcore.NewConsoleEncoder(encoderOption)
defaultErrorOutputOptions = []zap.Option{zap.ErrorOutput(defaultWriter)}
options := defaultErrorOutputOptions
if verbose {
options = append(options, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.PanicLevel)))
}
// reset log option slice
defaultOptions = getOptions(defaultWriter, verbose)
options = append(options, additionalOptions...)
log := initLogger(defaultWriter, mainLogName, formatter, level, defaultOptions...)
log := initLogger(defaultWriter, mainLogName, formatter, level, options...)
replaceGlobalLogger(log)
return log.Sugar()
}
func New(parent *zap.SugaredLogger, moduleName string) *zap.SugaredLogger {
func New(parent *zap.SugaredLogger, moduleName string, options ...zap.Option) *zap.SugaredLogger {
var subLogger *zap.Logger
if parent == nil {
return zap.L().Named(moduleName).Sugar()
subLogger = zap.L().Named(moduleName)
} else {
return parent.Named(moduleName)
subLogger = parent.Desugar().Named(moduleName)
}
subLogger.WithOptions(options...)
return subLogger.Sugar()
}
func NewOtherLogger(
@ -64,13 +72,28 @@ func NewOtherLogger(
logWriter := rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
core := zapcore.NewCore(formatter, logWriter, loglevel)
closer = logWriter.Close
options := []zap.Option{zap.ErrorOutput(defaultWriter)}
logger = zap.New(core, options...).
logger = zap.New(core, defaultErrorOutputOptions...).
Named(moduleName).With(fields...).Sugar()
return
}
func NewOtherLoggerWithOption(
formatter zapcore.Encoder,
moduleName, logFilename, logDir string,
maxSizeMb, maxBackup, maxDay int,
logLevel zapcore.Level,
options []zap.Option,
fields ...zapcore.Field,
) (logger *zap.SugaredLogger, closer func() error) {
loglevel := zap.NewAtomicLevelAt(logLevel)
logWriter := rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
core := zapcore.NewCore(formatter, logWriter, loglevel)
closer = logWriter.Close
options = append(defaultErrorOutputOptions, options...)
logger = zap.New(core, options...).
Named(moduleName).With(fields...).Sugar()
return
}
func initLogger(
writer zapcore.WriteSyncer,
moduleName string,
@ -81,12 +104,3 @@ func initLogger(
core := zapcore.NewCore(formatter, writer, level)
return zap.New(core, options...).Named(moduleName)
}
func getOptions(errWriter zapcore.WriteSyncer, verbose bool) (options []zap.Option) {
// define error log
options = append(options, zap.ErrorOutput(errWriter))
if verbose {
options = append(options, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.PanicLevel)))
}
return
}