Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ego日志通过配置调整时使用指针赋值,保证ego结构体内部的logger能够同步更新 #401

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
})
}
Loading