diff --git a/config.scfg b/config.scfg index 7da6bba..e00271f 100644 --- a/config.scfg +++ b/config.scfg @@ -5,6 +5,8 @@ base-url https://ntfy-alertmanager.xenrox.net http-address :8080 # Log level (either debug, info, warning, error) log-level info +# Log format (either text or json) +log-format text # When multiple alerts are grouped together by Alertmanager, they can either be sent # each on their own (single mode) or be kept together (multi mode) (either single or multi; default is multi) alert-mode single diff --git a/config/config.go b/config/config.go index a9312f0..e8a2db8 100644 --- a/config/config.go +++ b/config/config.go @@ -24,6 +24,7 @@ type Config struct { BaseURL string HTTPAddress string LogLevel string + LogFormat string AlertMode AlertMode User string Password string @@ -90,6 +91,7 @@ func ReadConfig(path string) (*Config, error) { // Set default values config.HTTPAddress = "127.0.0.1:8080" config.LogLevel = "info" + config.LogFormat = "text" config.AlertMode = Multi config.Cache.Type = "disabled" @@ -106,6 +108,13 @@ func ReadConfig(path string) (*Config, error) { } } + d = cfg.Get("log-format") + if d != nil { + if err := d.ParseParams(&config.LogFormat); err != nil { + return nil, err + } + } + d = cfg.Get("http-address") if d != nil { if err := d.ParseParams(&config.HTTPAddress); err != nil { diff --git a/config/config_test.go b/config/config_test.go index d337fc8..6795dba 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -13,6 +13,7 @@ func TestReadConfig(t *testing.T) { base-url https://ntfy-alertmanager.xenrox.net http-address :8080 log-level info +log-format json alert-mode multi user webhookUser password webhookPass @@ -66,6 +67,7 @@ cache { BaseURL: "https://ntfy-alertmanager.xenrox.net", HTTPAddress: ":8080", LogLevel: "info", + LogFormat: "json", AlertMode: Multi, User: "webhookUser", Password: "webhookPass", diff --git a/main.go b/main.go index ac1cc57..ce8b0c3 100644 --- a/main.go +++ b/main.go @@ -453,24 +453,19 @@ func main() { os.Exit(0) } - logLevel := new(slog.LevelVar) - logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ - Level: logLevel, - })) - cfg, err := config.ReadConfig(configPath) if err != nil { - logger.Error("Failed to read config", + slog.Error("Failed to read config", slog.String("error", err.Error())) os.Exit(1) } - level, err := logging.ParseLevelFromString(cfg.LogLevel) + logger, err := logging.New(cfg.LogLevel, cfg.LogFormat, os.Stderr) if err != nil { - logger.Error("Failed to parse logging level", + slog.Error("Failed to create logger", slog.String("error", err.Error())) + os.Exit(1) } - logLevel.Set(level) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop()