From 3a3c960951a7ad92aad32c606e6cc39ba65b8140 Mon Sep 17 00:00:00 2001 From: mircearoata Date: Sun, 4 Feb 2024 20:59:19 +0100 Subject: [PATCH] Fix logging not working when not running from terminal --- main.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index e05c834d..3f8d536d 100644 --- a/main.go +++ b/main.go @@ -208,11 +208,17 @@ func init() { _ = level.UnmarshalText([]byte(viper.GetString("log"))) handlers := make([]slog.Handler, 0) - handlers = append(handlers, tint.NewHandler(os.Stdout, &tint.Options{ - Level: level, - AddSource: true, - TimeFormat: time.RFC3339, - })) + + if _, err = os.Stdout.Stat(); err == nil { + // Only add the stdout handler if it is writable. + // Otherwise, the fanout handler would have the first handler error, + // and will not get to use the file handler. + handlers = append(handlers, tint.NewHandler(os.Stdout, &tint.Options{ + Level: level, + AddSource: true, + TimeFormat: time.RFC3339, + })) + } if viper.GetString("log-file") != "" { logFile := &lumberjack.Logger{