diff --git a/log.go b/log.go index 7fd2e24..c668975 100644 --- a/log.go +++ b/log.go @@ -35,6 +35,7 @@ type logConfig struct { CrashLogFilename string ErrorLogFilename string EnableLineNumber bool + AddCallerSkip int // enable the truncation of the level text to 4 characters. EnableLevelTruncation bool @@ -62,6 +63,10 @@ func (config *logConfig) Init() { logger = config.newLogger().Sugar() } +func (config *logConfig) New() *zap.SugaredLogger { + return config.newLogger().Sugar() +} + func NewLogConfig() *logConfig { return &logConfig{ Level: DebugLevel, @@ -113,7 +118,7 @@ func (config *logConfig) newLogger() *zap.Logger { var options []zap.Option if config.EnableLineNumber { - options = append(options, zap.AddCaller(), zap.AddCallerSkip(1)) + options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip)) } if config.EnableErrorStacktrace { @@ -265,3 +270,7 @@ func writeCrashLog(file string) { func Fields(args ...interface{}) { logger = logger.With(args...) } + +func With(l *zap.SugaredLogger, args ...interface{}) *zap.SugaredLogger { + return l.With(args...) +} diff --git a/log_test.go b/log_test.go index 648a62e..4b7354e 100644 --- a/log_test.go +++ b/log_test.go @@ -21,10 +21,10 @@ func TestLogWithConfig(t *testing.T) { config.Init() - With("traceid", float64(21221212122)) + Fields("traceid", float64(21221212122)) Debugf("this is %s message", "debug") config.Init() - With(zap.String("traceid", "12123123123")) + Fields(zap.String("traceid", "12123123123")) Infof("this is %s message", "info") // Errorf("this is %s message", "error") // Panicf("this is %s message", "panic") diff --git a/logger.go b/logger.go index fdced5d..c49c80c 100644 --- a/logger.go +++ b/logger.go @@ -1,9 +1,5 @@ package log -func With(args ...interface{}) { - logger = logger.With(args...) -} - func Debug(args ...interface{}) { logger.Debug(args...) } diff --git a/version b/version index e1d848b..6183501 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.0.8 \ No newline at end of file +v0.0.9 \ No newline at end of file