Skip to content

Commit

Permalink
ego日志通过配置调整时使用指针赋值,保证ego结构体内部的logger能够同步更新
Browse files Browse the repository at this point in the history
  • Loading branch information
jarily committed Jun 20, 2024
1 parent 2776f57 commit 40db192
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ego_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 26 additions & 0 deletions ego_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

0 comments on commit 40db192

Please sign in to comment.