Add option for setting the logging format
This commit is contained in:
parent
6d64c21549
commit
ddd41a6341
4 changed files with 17 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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",
|
||||
|
|
13
main.go
13
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()
|
||||
|
|
Loading…
Reference in a new issue