add json_format option for structure logging

This commit is contained in:
2025-08-02 00:00:35 +03:00
parent 8684d178e0
commit 6e59af1662
3 changed files with 10 additions and 1 deletions

View File

@@ -58,6 +58,7 @@ func (c *Compositor) LoadConf(path string) error {
v.SetDefault("updates.enabled", false) v.SetDefault("updates.enabled", false)
v.SetDefault("updates.check_interval", "2h") v.SetDefault("updates.check_interval", "2h")
v.SetDefault("updates.wanted_version", "latest-stable") v.SetDefault("updates.wanted_version", "latest-stable")
v.SetDefault("log.json_format", "false")
v.SetDefault("log.level", "info") v.SetDefault("log.level", "info")
v.SetDefault("log.output", "%stdout%") v.SetDefault("log.output", "%stdout%")

View File

@@ -55,6 +55,7 @@ type Updates struct {
} }
type Log struct { type Log struct {
JSON *bool `mapstructure:"json_format"`
Level *string `mapstructure:"level"` Level *string `mapstructure:"level"`
OutPath any `mapstructure:"output"` OutPath any `mapstructure:"output"`
} }

View File

@@ -90,6 +90,13 @@ func SetupLogger(o *config.Log) (*slog.Logger, error) {
writer = logFile writer = logFile
} }
log := slog.New(slog.NewJSONHandler(writer, &handlerOpts)) var handler slog.Handler
if *o.JSON {
handler = slog.NewJSONHandler(writer, &handlerOpts)
} else {
handler = slog.NewTextHandler(writer, &handlerOpts)
}
log := slog.New(handler)
return log, nil return log, nil
} }