diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2023-08-17 22:02:52 +0200 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2023-08-17 22:02:52 +0200 |
commit | 10f7eb53f4370cab6c1ff390773772389d500e59 (patch) | |
tree | 8c7d81f6428f182bec3162274d88fe4e486be9e1 /cmd/monfront/main.go | |
parent | a4a8c642296560ddb88ee43199d43520250b9828 (diff) |
prepare switch to log/slog
This commit prepares the switch from log to log/slog, which was
introduced in Go 1.21.
slog provides some useful facilities to add metadata to log entries,
which should be helpful for debugging problems.
This commit also adds a small transaction ID generator. It provides a
common identifier between log messages, so that multiple errors can be
viewed together in their order.
Diffstat (limited to 'cmd/monfront/main.go')
-rw-r--r-- | cmd/monfront/main.go | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/cmd/monfront/main.go b/cmd/monfront/main.go index a318e75..19be6e0 100644 --- a/cmd/monfront/main.go +++ b/cmd/monfront/main.go @@ -6,8 +6,10 @@ import ( "flag" "fmt" "html/template" + "io" "io/ioutil" "log" + "log/slog" "net" "net/http" "os" @@ -49,6 +51,11 @@ type ( Mode string `toml:"mode"` List []string `toml:"list"` } + Log struct { + Format string `toml:"format"` + Level string `toml:"level"` + Output string `toml:"output"` + } } MapEntry struct { @@ -99,6 +106,8 @@ func main() { log.Fatalf("could not parse config: %s", err) } + logger := parseLogger(config) + db, err := sql.Open("postgres", config.DB) if err != nil { log.Fatalf("could not open database connection: %s", err) @@ -168,7 +177,7 @@ func main() { l = tls.NewListener(l, tlsConf) } - s := newServer(l, db, tmpl, auth, autho) + s := newServer(l, db, logger, tmpl, auth, autho) s.Handle("/", showChecks) s.Handle("/create", showCreate) s.Handle("/check", showCheck) @@ -179,6 +188,48 @@ func main() { log.Fatalf("http server stopped: %s", s.ListenAndServe()) } +func parseLogger(config Config) *slog.Logger { + var output io.Writer + switch config.Log.Output { + case "", "stderr": + output = os.Stderr + case "stdout": + output = os.Stdout + default: + var err error + output, err = os.OpenFile(config.Log.Output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640) + if err != nil { + log.Fatalf("could not open log file handler: %s", err) + } + } + + var level slog.Level + switch config.Log.Level { + case "debug": + level = slog.LevelDebug + case "", "info": + level = slog.LevelInfo + case "warn": + level = slog.LevelWarn + case "error": + level = slog.LevelError + default: + log.Fatalf("unknown log level '%s', only 'debug', 'info', 'warn' and 'error' are supported", config.Log.Level) + } + + var handler slog.Handler + switch config.Log.Format { + case "", "text": + handler = slog.NewTextHandler(output, &slog.HandlerOptions{Level: level}) + case "json": + handler = slog.NewJSONHandler(output, &slog.HandlerOptions{Level: level}) + default: + log.Fatalf("unknown log format '%s', only 'text' and 'json' are supported", config.Log.Format) + } + + return slog.New(handler) +} + func checkAction(con *Context) { if con.r.Method != "POST" { con.w.WriteHeader(http.StatusMethodNotAllowed) |