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..7ed6430 100644 --- a/ego_function_test.go +++ b/ego_function_test.go @@ -230,4 +230,32 @@ 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() + // 先还原一下默认的EgoLogger + elog.EgoLogger = elog.DefaultContainer().Build(elog.WithFileName(elog.EgoLoggerName)) + 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()) + }) }