From ebae5c29cd3bdcd6f73ef3a2a1d4ef851c0ed742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Wed, 18 May 2022 18:53:17 +0800 Subject: [PATCH] :bug: Fix a bug. --- gorm_test.go | 3 ++- logger.go | 6 +++--- logger_test.go | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/gorm_test.go b/gorm_test.go index 7fdd8e2..6926b0d 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -1,6 +1,7 @@ package logger import ( + "gitee.ltd/lxh/logger/log" "gorm.io/driver/mysql" "gorm.io/gorm" "testing" @@ -11,7 +12,7 @@ func TestGormLogger(t *testing.T) { engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: DefaultGormLogger()}) if err != nil { - Say.Panicf("mysql connect error: %s", err.Error()) + log.Panicf("mysql connect error: %s", err.Error()) } var count int64 diff --git a/logger.go b/logger.go index a30fc78..9d2dc2b 100644 --- a/logger.go +++ b/logger.go @@ -8,11 +8,11 @@ import ( ) var config LogConfig -var Say *zap.SugaredLogger +var initialized bool // 避免异常,在第一次调用时初始化一个只打印到控制台的logger func init() { - if Say == nil { + if !initialized { // 从环境变量读取配置 var c LogConfig if err := env.Parse(&c); err != nil { @@ -48,7 +48,7 @@ func InitLogger(c LogConfig) { // 增加 caller 信息 // AddCallerSkip 输出的文件名和行号是调用封装函数的位置,而不是调用日志函数的位置 logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller(), zap.AddCallerSkip(1)) - Say = logger.Sugar() + initialized = true // 给GORM单独生成一个 gormZap = zap.New(zapcore.NewTee(cores...), zap.AddCaller(), zap.AddCallerSkip(3)).Sugar() zap.ReplaceGlobals(logger) diff --git a/logger_test.go b/logger_test.go index 0613aa3..ecc7aa3 100644 --- a/logger_test.go +++ b/logger_test.go @@ -8,11 +8,11 @@ import ( func TestLogger(t *testing.T) { InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true}) - Say.Debug("芜湖") + log.Debug("芜湖") } func TestLogger1(t *testing.T) { - Say.Info("我是测试消息") + log.Info("我是测试消息") time.Sleep(5 * time.Second) }