added option prameter
This commit is contained in:
parent
437417de21
commit
0a9f4c0597
|
@ -9,8 +9,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultOptions []zap.Option
|
|
||||||
defaultWriter logger.RotateSyncer
|
defaultWriter logger.RotateSyncer
|
||||||
|
defaultErrorOutputOptions []zap.Option
|
||||||
nopCloser = func() (err error) { return }
|
nopCloser = func() (err error) { return }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,28 +29,36 @@ func Init(
|
||||||
mainLogName, logFilename, logDir string,
|
mainLogName, logFilename, logDir string,
|
||||||
maxSizeMb, maxBackup, maxDay int,
|
maxSizeMb, maxBackup, maxDay int,
|
||||||
logLevel zapcore.Level,
|
logLevel zapcore.Level,
|
||||||
|
additionalOptions ...zap.Option,
|
||||||
) *zap.SugaredLogger {
|
) *zap.SugaredLogger {
|
||||||
level := zap.NewAtomicLevelAt(logLevel)
|
level := zap.NewAtomicLevelAt(logLevel)
|
||||||
defaultWriter = rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
|
defaultWriter = rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
|
||||||
|
|
||||||
//encoderOption := getEncoderOption(verbose)
|
defaultErrorOutputOptions = []zap.Option{zap.ErrorOutput(defaultWriter)}
|
||||||
//formatter := zapcore.NewConsoleEncoder(encoderOption)
|
options := defaultErrorOutputOptions
|
||||||
|
if verbose {
|
||||||
|
options = append(options, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.PanicLevel)))
|
||||||
|
}
|
||||||
// reset log option slice
|
// 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)
|
replaceGlobalLogger(log)
|
||||||
return log.Sugar()
|
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 {
|
if parent == nil {
|
||||||
return zap.L().Named(moduleName).Sugar()
|
subLogger = zap.L().Named(moduleName)
|
||||||
} else {
|
} else {
|
||||||
return parent.Named(moduleName)
|
subLogger = parent.Desugar().Named(moduleName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subLogger.WithOptions(options...)
|
||||||
|
|
||||||
|
return subLogger.Sugar()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOtherLogger(
|
func NewOtherLogger(
|
||||||
|
@ -64,13 +72,28 @@ func NewOtherLogger(
|
||||||
logWriter := rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
|
logWriter := rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
|
||||||
core := zapcore.NewCore(formatter, logWriter, loglevel)
|
core := zapcore.NewCore(formatter, logWriter, loglevel)
|
||||||
closer = logWriter.Close
|
closer = logWriter.Close
|
||||||
|
logger = zap.New(core, defaultErrorOutputOptions...).
|
||||||
options := []zap.Option{zap.ErrorOutput(defaultWriter)}
|
|
||||||
logger = zap.New(core, options...).
|
|
||||||
Named(moduleName).With(fields...).Sugar()
|
Named(moduleName).With(fields...).Sugar()
|
||||||
return
|
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(
|
func initLogger(
|
||||||
writer zapcore.WriteSyncer,
|
writer zapcore.WriteSyncer,
|
||||||
moduleName string,
|
moduleName string,
|
||||||
|
@ -81,12 +104,3 @@ func initLogger(
|
||||||
core := zapcore.NewCore(formatter, writer, level)
|
core := zapcore.NewCore(formatter, writer, level)
|
||||||
return zap.New(core, options...).Named(moduleName)
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue