Skip to content

Commit

Permalink
Merge pull request #401 from Jarily/master
Browse files Browse the repository at this point in the history
ego日志通过配置调整时使用指针赋值,保证ego结构体内部的logger能够同步更新
  • Loading branch information
askuy authored Jun 24, 2024
2 parents 2776f57 + 6c983d6 commit ed20329
Show file tree
Hide file tree
Showing 2 changed files with 30 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
28 changes: 28 additions & 0 deletions ego_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

0 comments on commit ed20329

Please sign in to comment.