瀏覽代碼

内存占用精简

jys 1 年之前
父節點
當前提交
302c9b1ab6
共有 3 個文件被更改,包括 33 次插入44 次删除
  1. 7 6
      benchmark_test.go
  2. 20 34
      log.go
  3. 6 4
      logger.go

+ 7 - 6
benchmark_test.go

@@ -7,22 +7,23 @@ import (
 
 // 性能对比(在进行仅控制台打印前提下):
 // fslog:
-// 63.29 ns/op           16 B/op          1 allocs/op
+// 32.50 ns/op           16 B/op          1 allocs/op
 // zap:
-// 10.10 ns/op            0 B/op          0 allocs/op
-// 在有额外文件输出时,fslog性能为:
-// 70.65 ns/op           16 B/op          1 allocs/op
+// 5.073 ns/op            0 B/op          0 allocs/op
 
 func BenchmarkFSLog(b *testing.B) {
-	SetLv(ErrorLv)
+	logger := New()
+	logger.SetLv(ErrorLv)
 	b.Run("fslog", func(b *testing.B) {
 		b.ReportAllocs()
 		for i := 0; i < b.N; i++ {
-			Debug("fslog")
+			logger.Debug("fslog")
 		}
 		b.StopTimer()
 	})
+}
 
+func BenchmarkZap(b *testing.B) {
 	production, err := zap.NewProduction()
 	if err != nil {
 		b.Fatal(err)

+ 20 - 34
log.go

@@ -10,29 +10,25 @@ import (
 
 var log *Logger
 
-// 默认配置,准备了控制台、json文件两种输出方式
-var consoleEncoder zapcore.Encoder
-var jsonEncoder zapcore.Encoder
-var consoleSync zapcore.WriteSyncer
-var outputSync zapcore.WriteSyncer
-
 func init() {
-	consoleEncoderConfig := zap.NewDevelopmentEncoderConfig()
-	consoleEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
-	consoleEncoder = zapcore.NewConsoleEncoder(consoleEncoderConfig)
-	jsonEncoderConfig := zap.NewProductionEncoderConfig()
-	jsonEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
-	jsonEncoder = zapcore.NewJSONEncoder(jsonEncoderConfig)
-	consoleSync = zapcore.AddSync(os.Stdout)
-	outputSync = zapcore.AddSync(&lumberjack.Logger{
-		Filename:  "logs/log",
-		MaxSize:   1,
-		MaxAge:    10,
-		LocalTime: true,
-	})
-
-	log = New()
-	log.AddCore(BuiltinConsoleCore(DebugLv), BuiltinOutputFileCore(DebugLv))
+	consoleCfg := zap.NewDevelopmentEncoderConfig()
+	consoleCfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
+	jsonCfg := zap.NewProductionEncoderConfig()
+	jsonCfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
+
+	log = &Logger{
+		lv: zap.NewAtomicLevelAt(zap.DebugLevel),
+	}
+	log.cores = append(log.cores,
+		zapcore.NewCore(zapcore.NewConsoleEncoder(consoleCfg), zapcore.AddSync(os.Stdout), log.lv),
+		zapcore.NewCore(zapcore.NewJSONEncoder(jsonCfg), zapcore.AddSync(&lumberjack.Logger{
+			Filename:  "logs/log",
+			MaxSize:   1,
+			MaxAge:    10,
+			LocalTime: true,
+		}), log.lv),
+	)
+	log.flushLogger()
 }
 
 // With 见 Logger.With
@@ -66,7 +62,7 @@ func Error(vs ...any) *Logger {
 
 // Lv 见 Logger.Lv
 func Lv() Level {
-	return Level(log.Lv().Level())
+	return log.Lv()
 }
 
 // SetLv 见 Logger.SetLv
@@ -88,7 +84,7 @@ func NewOutput(writer io.Writer) *Logger {
 }
 
 // SetLogger 替换fslog包默认的log对象
-// Logger 支持了很多zapcore级别的底层配置,见 Logger
+// Logger 支持了zapcore级别的底层配置,见 Logger
 func SetLogger(logger *Logger) *Logger {
 	log = logger
 	return log
@@ -98,13 +94,3 @@ func SetLogger(logger *Logger) *Logger {
 func Flush() {
 	log.Flush()
 }
-
-// BuiltinConsoleCore 获取内置的控制台输出 zapcore.Core
-func BuiltinConsoleCore(lv Level) zapcore.Core {
-	return zapcore.NewCore(consoleEncoder, consoleSync, lv.zap())
-}
-
-// BuiltinOutputFileCore 获取文件输出 zapcore.Core
-func BuiltinOutputFileCore(lv Level) zapcore.Core {
-	return zapcore.NewCore(jsonEncoder, outputSync, lv.zap())
-}

+ 6 - 4
logger.go

@@ -58,7 +58,7 @@ func (l *Logger) Debug(msg string, vs ...any) {
 		l.logger.Debug(msg)
 		return
 	}
-	l.logger.Debugf(fmt.Sprintf(msg, vs...))
+	l.logger.Debugf(msg, vs...)
 }
 
 // Info 格式化打印信息级别日志
@@ -124,8 +124,8 @@ func (l *Logger) Error(vs ...any) {
 }
 
 // Lv 获取当前日志打印级别
-func (l *Logger) Lv() zap.AtomicLevel {
-	return l.lv
+func (l *Logger) Lv() Level {
+	return Level(l.lv.Level())
 }
 
 // SetLv 设置当前日志打印级别
@@ -151,7 +151,9 @@ func (l *Logger) NewFileOutput(opts ...FileOutputOpt) {
 
 // NewOutput 新增日志输出位置
 func (l *Logger) NewOutput(writer io.Writer) {
-	l.AddCore(zapcore.NewCore(jsonEncoder, zapcore.AddSync(writer), l.lv))
+	cfg := zap.NewProductionEncoderConfig()
+	cfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
+	l.AddCore(zapcore.NewCore(zapcore.NewJSONEncoder(cfg), zapcore.AddSync(writer), l.lv))
 }
 
 // AddCore 添加Core