From 10f7eb53f4370cab6c1ff390773772389d500e59 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 17 Aug 2023 22:02:52 +0200 Subject: 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. --- cmd/monfront/main.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'cmd/monfront/main.go') 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) -- cgit v1.2.3-70-g09d2