From 40db192eb19f648f9a336bcefa65a23d5ffb9506 Mon Sep 17 00:00:00 2001 From: Jarily <11690345@qq.com> Date: Thu, 20 Jun 2024 11:36:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ego=E6=97=A5=E5=BF=97=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=B0=83=E6=95=B4=E6=97=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=8C=87=E9=92=88=E8=B5=8B=E5=80=BC=EF=BC=8C=E4=BF=9D=E8=AF=81?= =?UTF-8?q?ego=E7=BB=93=E6=9E=84=E4=BD=93=E5=86=85=E9=83=A8=E7=9A=84logger?= =?UTF-8?q?=E8=83=BD=E5=A4=9F=E5=90=8C=E6=AD=A5=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ego_function.go | 4 ++-- ego_function_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ego_function.go b/ego_function.go index f23c981..0d341b5 100644 --- a/ego_function.go +++ b/ego_function.go @@ -229,13 +229,13 @@ func loadConfig() error { // initLogger init application and Ego logger func (e *Ego) initLogger() error { if econf.Get(e.opts.configPrefix+"logger.default") != nil { - elog.DefaultLogger = elog.Load(e.opts.configPrefix + "logger.default").Build(elog.WithCallSkip(2)) // DefaultLogger 默认为2层 + *(elog.DefaultLogger) = *(elog.Load(e.opts.configPrefix + "logger.default").Build(elog.WithCallSkip(2))) // DefaultLogger 默认为2层 elog.EgoLogger.Info("reinit default logger", elog.FieldComponent(elog.PackageName)) e.opts.afterStopClean = append(e.opts.afterStopClean, elog.DefaultLogger.Flush) } if econf.Get(e.opts.configPrefix+"logger.ego") != nil { - elog.EgoLogger = elog.Load(e.opts.configPrefix + "logger.ego").Build(elog.WithDefaultFileName(elog.EgoLoggerName)) + *(elog.EgoLogger) = *(elog.Load(e.opts.configPrefix + "logger.ego").Build(elog.WithDefaultFileName(elog.EgoLoggerName))) elog.EgoLogger.Info("reinit ego logger", elog.FieldComponent(elog.PackageName)) e.opts.afterStopClean = append(e.opts.afterStopClean, elog.EgoLogger.Flush) } diff --git a/ego_function_test.go b/ego_function_test.go index 6308be2..3fac2a5 100644 --- a/ego_function_test.go +++ b/ego_function_test.go @@ -230,4 +230,30 @@ func Test_initSysLogger(t *testing.T) { // 验证日志文件名是否为ego.sys.log assert.Equal(t, fileName, elog.EgoLogger.ConfigName()) }) + + t.Run("修改EgoLogger本身,ego中的logger同步生效", func(t *testing.T) { + econf.Reset() + var ( + app = &Ego{ + logger: elog.EgoLogger, // logger与ego.New()方法中保持一致 + } + cfg = ` + [logger.ego] + debug = true + name = "ego.sys.log" + ` + ) + + err := econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal) + assert.NoError(t, err) + + // 初始化之前使用的默认的name + assert.Equal(t, elog.EgoLoggerName, app.logger.ConfigName()) + + err1 := app.initLogger() + assert.NoError(t, err1) + + // 初始化后验证ego结构体中的logger日志文件名是否为ego.sys.log + assert.Equal(t, "ego.sys.log", app.logger.ConfigName()) + }) } From 6c983d685564f1d41f2396110096755751a9c0e6 Mon Sep 17 00:00:00 2001 From: Jarily <11690345@qq.com> Date: Mon, 24 Jun 2024 10:22:03 +0800 Subject: [PATCH 2/2] fix unit test --- ego_function_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ego_function_test.go b/ego_function_test.go index 3fac2a5..7ed6430 100644 --- a/ego_function_test.go +++ b/ego_function_test.go @@ -233,6 +233,8 @@ func Test_initSysLogger(t *testing.T) { t.Run("修改EgoLogger本身,ego中的logger同步生效", func(t *testing.T) { econf.Reset() + // 先还原一下默认的EgoLogger + elog.EgoLogger = elog.DefaultContainer().Build(elog.WithFileName(elog.EgoLoggerName)) var ( app = &Ego{ logger: elog.EgoLogger, // logger与ego.New()方法中保持一致