From 1c7341499a0fc32da3590498dcf5037d8e15723c Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Fri, 26 Sep 2025 21:12:29 +0200 Subject: [PATCH] switch to json slog --- dev.sh | 2 +- main.go | 18 ++++++++++++++---- pages/home.gmi | 2 ++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dev.sh b/dev.sh index 947d997..23a241e 100755 --- a/dev.sh +++ b/dev.sh @@ -2,4 +2,4 @@ set -e -go run main.go -cert=./keys/cert.crt.pem -key=./keys/localhost_key.pem -host=localhost:8080 +go run main.go -cert=./keys/localhost.crt.pem -key=./keys/localhost_key.pem -host=localhost:8080 diff --git a/main.go b/main.go index e139a6b..69ece37 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,8 @@ import ( "errors" "flag" "fmt" - "log" + "log/slog" + "os" "strconv" "strings" "time" @@ -16,7 +17,9 @@ type ExampleHandler struct { } func (h ExampleHandler) ServeGemini(w gemini.ResponseWriter, req *gemini.Request) { - log.Printf("request: %s, user: %v", req.URL.Path, strings.Join(userName(req), " ")) + slog.Info("gemini request", + "path", req.URL.Path, + "user", strings.Join(userName(req), " ")) switch req.URL.Path { case "/": gemini.ServeFileName("pages/home.gmi", "text/gemini")(w, req) @@ -76,7 +79,13 @@ func userName(r *gemini.Request) []string { } func main() { - println("Starting gemini server") + // Set up structured JSON logging + logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.LevelInfo, + })) + slog.SetDefault(logger) + + slog.Info("Starting gemini server") var host, cert, key string flag.StringVar(&host, "host", ":1965", "listen on host and port. Example: hostname:1965") flag.StringVar(&cert, "cert", "server.crt.pem", "certificate file") @@ -87,6 +96,7 @@ func main() { err := gemini.ListenAndServe(host, cert, key, gemini.TrapPanic(handler.ServeGemini)) if err != nil { - log.Fatal(err) + slog.Error("server failed to start", "error", err) + os.Exit(1) } } diff --git a/pages/home.gmi b/pages/home.gmi index 6a1a03e..7ac5864 100644 --- a/pages/home.gmi +++ b/pages/home.gmi @@ -48,3 +48,5 @@ Took quite some tinkering to get everything working, but it's finally up and run You can find the source code for this capsule here: => https://git.travisshears.com/travisshears/personal-gemini-capsule +Diagram created with: +=> https://asciiflow.com