From 84934eff541c99e3cca802dd2dff7e2d34135a84 Mon Sep 17 00:00:00 2001 From: steiler Date: Fri, 22 May 2026 09:53:38 +0200 Subject: [PATCH] Adding the EXTRA_LOG_FILE env variable that enables writing logs to a file, in addition to just writing it to stdout. Purpose is to use debugging mode in e.g. VSCode but use [loganalyzer](https://github.com/steiler/loganalyzer) at the same time. --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 717e8315..fe7a889d 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "context" "encoding/json" "fmt" + "io" "log/slog" "os" "os/signal" @@ -66,7 +67,17 @@ func main() { slogOpts.Level = slog.Level(-logf.VTrace) } - log := logr.FromSlogHandler(slog.NewJSONHandler(os.Stdout, slogOpts)) + var output io.Writer = os.Stdout + if logFile := os.Getenv("EXTRA_LOG_FILE"); logFile != "" { + f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to open log file %s: %v\n", logFile, err) + } else { + output = io.MultiWriter(os.Stdout, f) + } + } + + log := logr.FromSlogHandler(slog.NewJSONHandler(output, slogOpts)) logf.SetDefaultLogger(log) ctx := logf.IntoContext(context.Background(), log)