From eab80e38778fcfc8c49184b4ed3a519a5548ead4 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Tue, 3 Aug 2021 07:25:05 -0700 Subject: [PATCH] logpolicy: only log panics when running under systemd Given that https://github.com/golang/go/issues/42888 is coming, this catches most practical panics without interfering in our development environments. Signed-off-by: David Crawshaw --- logpolicy/logpolicy.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/logpolicy/logpolicy.go b/logpolicy/logpolicy.go index 62dc92a89..aa6a7ffb2 100644 --- a/logpolicy/logpolicy.go +++ b/logpolicy/logpolicy.go @@ -187,6 +187,10 @@ func runningUnderSystemd() bool { return false } +func redirectStderrToLogPanics() bool { + return runningUnderSystemd() || os.Getenv("TS_PLEASE_PANIC") != "" +} + // tryFixLogStateLocation is a temporary fixup for // https://github.com/tailscale/tailscale/issues/247 . We accidentally // wrote logging state files to /, and then later to $CACHE_DIRECTORY @@ -436,11 +440,13 @@ func New(collection string) *Policy { } filchBuf, filchErr := filch.New(filepath.Join(dir, cmdName), filch.Options{ - ReplaceStderr: true, + ReplaceStderr: redirectStderrToLogPanics(), }) if filchBuf != nil { c.Buffer = filchBuf - c.Stderr = filchBuf.OrigStderr + if filchBuf.OrigStderr != nil { + c.Stderr = filchBuf.OrigStderr + } } lw := logtail.NewLogger(c, log.Printf) log.SetFlags(0) // other logflags are set on console, not here