Support logging to file
This commit is contained in:
parent
a1d620b6d2
commit
f2cac349c6
4 changed files with 29 additions and 1 deletions
|
@ -19,6 +19,9 @@ log-level info
|
||||||
# Options: text, json
|
# Options: text, json
|
||||||
# Default: text
|
# Default: text
|
||||||
log-format text
|
log-format text
|
||||||
|
# Write logs to this file. If unset, the logs will be written to stderr.
|
||||||
|
# Default: ""
|
||||||
|
log-file /var/log/ntfy-alertmanager.log
|
||||||
# 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)
|
# each on their own (single mode) or be kept together (multi mode)
|
||||||
# Options: single, multi
|
# Options: single, multi
|
||||||
|
|
|
@ -25,6 +25,7 @@ type Config struct {
|
||||||
HTTPAddress string
|
HTTPAddress string
|
||||||
LogLevel string
|
LogLevel string
|
||||||
LogFormat string
|
LogFormat string
|
||||||
|
LogFile string
|
||||||
AlertMode AlertMode
|
AlertMode AlertMode
|
||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
|
@ -97,6 +98,13 @@ func parseBlock(block scfg.Block, config *Config) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d = block.Get("log-file")
|
||||||
|
if d != nil {
|
||||||
|
if err := d.ParseParams(&config.LogFile); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
d = block.Get("http-address")
|
d = block.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 {
|
||||||
|
|
|
@ -14,6 +14,7 @@ base-url https://ntfy-alertmanager.example.com
|
||||||
http-address :8080
|
http-address :8080
|
||||||
log-level info
|
log-level info
|
||||||
log-format json
|
log-format json
|
||||||
|
log-file /var/log/ntfy-alertmanager.log
|
||||||
alert-mode multi
|
alert-mode multi
|
||||||
user webhookUser
|
user webhookUser
|
||||||
password webhookPass
|
password webhookPass
|
||||||
|
@ -70,6 +71,7 @@ cache {
|
||||||
HTTPAddress: ":8080",
|
HTTPAddress: ":8080",
|
||||||
LogLevel: "info",
|
LogLevel: "info",
|
||||||
LogFormat: "json",
|
LogFormat: "json",
|
||||||
|
LogFile: "/var/log/ntfy-alertmanager.log",
|
||||||
AlertMode: Multi,
|
AlertMode: Multi,
|
||||||
User: "webhookUser",
|
User: "webhookUser",
|
||||||
Password: "webhookPass",
|
Password: "webhookPass",
|
||||||
|
|
17
main.go
17
main.go
|
@ -14,6 +14,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -505,7 +506,21 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := logging.New(cfg.LogLevel, cfg.LogFormat, os.Stderr)
|
var logOutput io.Writer
|
||||||
|
if cfg.LogFile != "" {
|
||||||
|
f, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to open logfile",
|
||||||
|
slog.String("error", err.Error()))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
logOutput = f
|
||||||
|
} else {
|
||||||
|
logOutput = os.Stderr
|
||||||
|
}
|
||||||
|
|
||||||
|
logger, err := logging.New(cfg.LogLevel, cfg.LogFormat, logOutput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to create logger",
|
slog.Error("Failed to create logger",
|
||||||
slog.String("error", err.Error()))
|
slog.String("error", err.Error()))
|
||||||
|
|
Loading…
Add table
Reference in a new issue