fix
This commit is contained in:
+25
-12
@@ -49,6 +49,20 @@ func newApp(logger *slog.Logger, hs *kratoshttp.Server, scheduler *worker.TaskSc
|
||||
)
|
||||
}
|
||||
|
||||
func zapSettings(admin *conf.AdminBackend) (string, logging.Options) {
|
||||
options := logging.Options{Level: "info", Format: "json", EncodeLevel: "LowercaseLevelEncoder", LogInConsole: true, ShowLine: true, RetentionDay: 7}
|
||||
root := "logs"
|
||||
if admin == nil || admin.Zap == nil {
|
||||
return root, options
|
||||
}
|
||||
zapConfig := admin.Zap
|
||||
options = logging.Options{Level: zapConfig.Level, Format: zapConfig.Format, EncodeLevel: zapConfig.EncodeLevel, Prefix: zapConfig.Prefix, StacktraceKey: zapConfig.StacktraceKey, LogInConsole: zapConfig.LogInConsole, ShowLine: zapConfig.ShowLine, RetentionDay: int(zapConfig.RetentionDay), FileOnlyModules: zapConfig.FileOnlyModules}
|
||||
if zapConfig.Director != "" {
|
||||
root = zapConfig.Director
|
||||
}
|
||||
return root, options
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
c := config.New(
|
||||
@@ -66,21 +80,13 @@ func main() {
|
||||
if err := c.Scan(&bc); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logOptions := logging.Options{Level: "info", Format: "json", EncodeLevel: "LowercaseLevelEncoder", LogInConsole: true, ShowLine: true, RetentionDay: 7}
|
||||
logRoot := "logs"
|
||||
if bc.Admin != nil && bc.Admin.Zap != nil {
|
||||
zapConfig := bc.Admin.Zap
|
||||
logOptions = logging.Options{Level: zapConfig.Level, Format: zapConfig.Format, EncodeLevel: zapConfig.EncodeLevel, Prefix: zapConfig.Prefix, StacktraceKey: zapConfig.StacktraceKey, LogInConsole: zapConfig.LogInConsole, ShowLine: zapConfig.ShowLine, RetentionDay: int(zapConfig.RetentionDay), FileOnlyModules: zapConfig.FileOnlyModules}
|
||||
if zapConfig.Director != "" {
|
||||
logRoot = zapConfig.Director
|
||||
}
|
||||
}
|
||||
logRoot, logOptions := zapSettings(bc.Admin)
|
||||
loggerAttrs := []any{slog.String("service.id", id), slog.String("service.name", Name), slog.String("service.version", Version)}
|
||||
if bc.Admin != nil && bc.Admin.App != nil {
|
||||
loggerAttrs = append(loggerAttrs, slog.String("node", bc.Admin.App.Node), slog.String("app_id", bc.Admin.App.AppId), slog.String("env", bc.Admin.App.Env))
|
||||
}
|
||||
logger, closeLogger := logging.NewZapLogger(logRoot, "application.log", logOptions, loggerAttrs...)
|
||||
defer closeLogger()
|
||||
logger, loggerControl := logging.NewReloadableZapLogger(logRoot, "application.log", logOptions, loggerAttrs...)
|
||||
defer loggerControl.Close()
|
||||
log.SetDefault(logger)
|
||||
if bc.Admin != nil {
|
||||
bc.Admin.ConfigPath = flagconf
|
||||
@@ -89,7 +95,14 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
app, cleanup, err := wireApp(bc.Server, bc.Data, bc.Admin, logger)
|
||||
runtime := conf.NewRuntime(bc.Data, bc.Admin)
|
||||
unsubscribeLogger := runtime.Subscribe(func(_ *conf.Data, admin *conf.AdminBackend) {
|
||||
root, options := zapSettings(admin)
|
||||
loggerControl.Reload(root, options)
|
||||
})
|
||||
defer unsubscribeLogger()
|
||||
|
||||
app, cleanup, err := wireApp(bc.Server, runtime, logger)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user