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
|
http-address :8080
|
||||||
# Log level (either debug, info, warning, error)
|
# Log level (either debug, info, warning, error)
|
||||||
log-level info
|
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
|
# 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)
|
# each on their own (single mode) or be kept together (multi mode) (either single or multi; default is multi)
|
||||||
alert-mode single
|
alert-mode single
|
||||||
|
|
|
@ -24,6 +24,7 @@ type Config struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
HTTPAddress string
|
HTTPAddress string
|
||||||
LogLevel string
|
LogLevel string
|
||||||
|
LogFormat string
|
||||||
AlertMode AlertMode
|
AlertMode AlertMode
|
||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
|
@ -90,6 +91,7 @@ func ReadConfig(path string) (*Config, error) {
|
||||||
// Set default values
|
// Set default values
|
||||||
config.HTTPAddress = "127.0.0.1:8080"
|
config.HTTPAddress = "127.0.0.1:8080"
|
||||||
config.LogLevel = "info"
|
config.LogLevel = "info"
|
||||||
|
config.LogFormat = "text"
|
||||||
config.AlertMode = Multi
|
config.AlertMode = Multi
|
||||||
|
|
||||||
config.Cache.Type = "disabled"
|
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")
|
d = cfg.Get("http-address")
|
||||||
if d != nil {
|
if d != nil {
|
||||||
if err := d.ParseParams(&config.HTTPAddress); err != 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
|
base-url https://ntfy-alertmanager.xenrox.net
|
||||||
http-address :8080
|
http-address :8080
|
||||||
log-level info
|
log-level info
|
||||||
|
log-format json
|
||||||
alert-mode multi
|
alert-mode multi
|
||||||
user webhookUser
|
user webhookUser
|
||||||
password webhookPass
|
password webhookPass
|
||||||
|
@ -66,6 +67,7 @@ cache {
|
||||||
BaseURL: "https://ntfy-alertmanager.xenrox.net",
|
BaseURL: "https://ntfy-alertmanager.xenrox.net",
|
||||||
HTTPAddress: ":8080",
|
HTTPAddress: ":8080",
|
||||||
LogLevel: "info",
|
LogLevel: "info",
|
||||||
|
LogFormat: "json",
|
||||||
AlertMode: Multi,
|
AlertMode: Multi,
|
||||||
User: "webhookUser",
|
User: "webhookUser",
|
||||||
Password: "webhookPass",
|
Password: "webhookPass",
|
||||||
|
|
13
main.go
13
main.go
|
@ -453,24 +453,19 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
logLevel := new(slog.LevelVar)
|
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
|
||||||
Level: logLevel,
|
|
||||||
}))
|
|
||||||
|
|
||||||
cfg, err := config.ReadConfig(configPath)
|
cfg, err := config.ReadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to read config",
|
slog.Error("Failed to read config",
|
||||||
slog.String("error", err.Error()))
|
slog.String("error", err.Error()))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
level, err := logging.ParseLevelFromString(cfg.LogLevel)
|
logger, err := logging.New(cfg.LogLevel, cfg.LogFormat, os.Stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to parse logging level",
|
slog.Error("Failed to create logger",
|
||||||
slog.String("error", err.Error()))
|
slog.String("error", err.Error()))
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
logLevel.Set(level)
|
|
||||||
|
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
Loading…
Reference in a new issue