Bladeren bron

caller日志打印位置优化
fslog的logger对象定制化扩展

jys 11 maanden geleden
bovenliggende
commit
deb4d3fa52
5 gewijzigde bestanden met toevoegingen van 50 en 11 verwijderingen
  1. 2 1
      log.go
  2. 14 4
      log_test.go
  3. 21 6
      logger.go
  4. 0 0
      opt_file.go
  5. 13 0
      opt_logger.go

+ 2 - 1
log.go

@@ -17,7 +17,8 @@ func init() {
 	jsonCfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
 
 	log = &Logger{
-		lv: zap.NewAtomicLevelAt(zap.DebugLevel),
+		lv:           zap.NewAtomicLevelAt(zap.DebugLevel),
+		loggerConfig: &loggerConfig{SkipCaller: 2},
 	}
 	log.cores = append(log.cores,
 		zapcore.NewCore(zapcore.NewConsoleEncoder(consoleCfg), zapcore.AddSync(os.Stdout), log.lv),

+ 14 - 4
log_test.go

@@ -59,7 +59,7 @@ func TestDebug(t *testing.T) {
 		t.Error("debug all message not match")
 	}
 
-	if !strings.Contains(out, "logger.go") {
+	if !strings.Contains(out, "fslog/log_test.go") {
 		t.Error("debug caller not match")
 	}
 }
@@ -87,7 +87,7 @@ func TestInfo(t *testing.T) {
 		t.Error("info fmt all message not match")
 	}
 
-	if !strings.Contains(out, "logger.go") {
+	if !strings.Contains(out, "fslog/log_test.go") {
 		t.Error("info caller not match")
 	}
 }
@@ -115,7 +115,7 @@ func TestWarn(t *testing.T) {
 		t.Error("warn fmt all message not match")
 	}
 
-	if !strings.Contains(out, "logger.go") {
+	if !strings.Contains(out, "fslog/log_test.go") {
 		t.Error("warn caller not match")
 	}
 }
@@ -143,7 +143,7 @@ func TestError(t *testing.T) {
 		t.Error("error fmt all message not match")
 	}
 
-	if !strings.Contains(out, "logger.go") {
+	if !strings.Contains(out, "fslog/log_test.go") {
 		t.Error("error caller not match")
 	}
 }
@@ -180,3 +180,13 @@ func TestFileExtend(t *testing.T) {
 		Info(strconv.Itoa(i))
 	}
 }
+
+func TestPrintLocation(t *testing.T) {
+	defer Flush()
+	buffer := &bytes.Buffer{}
+	NewOutput(buffer)
+	Info("test location")
+	if !strings.Contains(buffer.String(), `"caller":"fslog/log_test.go:`) {
+		t.Error("caller output have deviation")
+	}
+}

+ 21 - 6
logger.go

@@ -35,10 +35,19 @@ type Logger struct {
 	cores []zapcore.Core
 
 	writers []io.Writer
+	*loggerConfig
 }
 
-func New() *Logger {
+func New(opts ...LoggerOption) *Logger {
 	logger := new(Logger)
+	logger.loggerConfig = new(loggerConfig)
+	if len(opts) == 0 {
+		opts = []LoggerOption{WithSkipCaller(1)}
+	}
+	for _, opt := range opts {
+		opt(logger.loggerConfig)
+	}
+
 	logger.lv = zap.NewAtomicLevelAt(zap.DebugLevel)
 	logger.flushLogger()
 	return logger
@@ -181,7 +190,7 @@ func (l *Logger) addCoreOnly(core ...zapcore.Core) {
 }
 
 func (l *Logger) clone() *Logger {
-	newL := &Logger{cores: l.cores, lv: l.lv}
+	newL := &Logger{cores: l.cores, lv: l.lv, loggerConfig: l.loggerConfig}
 	newL.flushLogger()
 	return newL
 }
@@ -191,8 +200,14 @@ func (l *Logger) clone() *Logger {
 func (l *Logger) flushLogger() {
 	l.lock.Lock()
 	defer l.lock.Unlock()
-	l.logger = *zap.New(
-		zapcore.NewTee(l.cores...),
-		zap.AddCaller(),
-	).Sugar()
+	l.logger = *zap.
+		New(
+			zapcore.NewTee(l.cores...),
+			zap.AddCaller(),
+		).
+		Sugar().
+		WithOptions(
+			zap.AddCallerSkip(l.SkipCaller),
+			zap.WithCaller(true),
+		)
 }

+ 0 - 0
opt.go → opt_file.go


+ 13 - 0
opt_logger.go

@@ -0,0 +1,13 @@
+package fslog
+
+type loggerConfig struct {
+	SkipCaller int
+}
+
+type LoggerOption func(*loggerConfig)
+
+func WithSkipCaller(skip int) LoggerOption {
+	return func(config *loggerConfig) {
+		config.SkipCaller = skip
+	}
+}