Преглед на файлове

调整代码结构,新封装zap core相关操作,方便后续扩展

jys преди 1 година
родител
ревизия
145c6d733c
променени са 2 файла, в които са добавени 54 реда и са изтрити 21 реда
  1. 21 0
      core.go
  2. 33 21
      logger.go

+ 21 - 0
core.go

@@ -0,0 +1,21 @@
+package fslog
+
+import (
+	"go.uber.org/zap/zapcore"
+	"io"
+)
+
+type CoreContext struct {
+	zapcore.Core
+	io.Writer
+}
+
+type coreContexts []CoreContext
+
+func (c coreContexts) Cores() []zapcore.Core {
+	cores := make([]zapcore.Core, len(c))
+	for i, core := range c {
+		cores[i] = core.Core
+	}
+	return cores
+}

+ 33 - 21
logger.go

@@ -38,14 +38,20 @@ type Logger struct {
 	lv zap.AtomicLevel
 
 	// zapcore.Core 映射,存储不同来源/用途的core创建
-	cores map[coreType][]zapcore.Core
+	cores map[coreType][]CoreContext
 }
 
 func NewLogger() *Logger {
 	logger := new(Logger)
 	logger.lv = zap.NewAtomicLevelAt(zap.DebugLevel)
-	logger.setCore(console, zapcore.NewCore(consoleEncoder, consoleSync, logger.lv))
-	logger.setCore(output, zapcore.NewCore(jsonEncoder, outputSync, logger.lv))
+	logger.setCore(console, CoreContext{
+		Core:   zapcore.NewCore(consoleEncoder, consoleSync, logger.lv),
+		Writer: consoleSync,
+	})
+	logger.setCore(output, CoreContext{
+		Core:   zapcore.NewCore(jsonEncoder, outputSync, logger.lv),
+		Writer: outputSync,
+	})
 	logger.flush()
 	return logger
 }
@@ -141,23 +147,34 @@ func (l *Logger) SetLv(lv Level) {
 	l.lv.SetLevel(lv.zap())
 }
 
+// NewFileOutput 新增日志输出文件配置
 func (l *Logger) NewFileOutput(opts ...FileOutputOpt) {
 	cfg := new(outFileConfig)
 	for _, opt := range opts {
 		opt(cfg)
 	}
-	l.newFileOut(*cfg)
+	l.newOut(&lumberjack.Logger{
+		Filename:   cfg.filename,
+		MaxSize:    cfg.maxSize,
+		MaxAge:     cfg.maxAge,
+		MaxBackups: cfg.maxBackups,
+		LocalTime:  cfg.localTime,
+		Compress:   cfg.Compress,
+	})
 }
 
+// NewOutput 新增日志输出位置
 func (l *Logger) NewOutput(writer io.Writer) {
 	l.newOut(writer)
 }
 
-func (l *Logger) SetCore(core ...zapcore.Core) {
+// AddCore 添加Core
+func (l *Logger) AddCore(core ...CoreContext) {
 	l.setCore(third, core...)
 	l.flush()
 }
 
+// Flush 将缓冲区日志刷新至目标
 func (l *Logger) Flush() {
 	err := l.logger.Sync()
 	if err != nil {
@@ -165,26 +182,21 @@ func (l *Logger) Flush() {
 	}
 }
 
-func (l *Logger) newFileOut(cfg outFileConfig) {
-	l.newOut(&lumberjack.Logger{
-		Filename:   cfg.filename,
-		MaxSize:    cfg.maxSize,
-		MaxAge:     cfg.maxAge,
-		MaxBackups: cfg.maxBackups,
-		LocalTime:  cfg.localTime,
-		Compress:   cfg.Compress,
-	})
-}
-
+// newOut 设置日志写出位置
 func (l *Logger) newOut(writer io.Writer) {
-	l.setCore(output, zapcore.NewCore(jsonEncoder, zapcore.AddSync(writer), l.lv))
+	l.setCore(output, CoreContext{
+		Core:   zapcore.NewCore(jsonEncoder, zapcore.AddSync(writer), l.lv),
+		Writer: writer,
+	})
 	l.flush()
 }
 
 // 保存内部core(不负责刷新 logger)
-func (l *Logger) setCore(ct coreType, core ...zapcore.Core) {
+func (l *Logger) setCore(ct coreType, core ...CoreContext) {
+	l.lock.Lock()
+	defer l.lock.Unlock()
 	if l.cores == nil {
-		l.cores = make(map[coreType][]zapcore.Core)
+		l.cores = make(map[coreType][]CoreContext)
 	}
 	l.cores[ct] = append(l.cores[ct], core...)
 }
@@ -195,13 +207,13 @@ func (l *Logger) flush() {
 	l.lock.Lock()
 	defer l.lock.Unlock()
 
-	cores := make([]zapcore.Core, 0, len(l.cores))
+	cores := make(coreContexts, 0, len(l.cores))
 	for _, core := range l.cores {
 		cores = append(cores, core...)
 	}
 
 	l.logger = *zap.New(
-		zapcore.NewTee(cores...),
+		zapcore.NewTee(cores.Cores()...),
 		zap.AddCaller(),
 	).Sugar()
 }