Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slog): disable automatic logging of stacktraces for errors #464

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions handlers/paniccheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log/slog"
"net/http"
"runtime"
"runtime/debug"

"github.com/urfave/negroni/v3"

Expand Down Expand Up @@ -41,7 +41,7 @@ func (p *panicCheck) ServeHTTP(rw http.ResponseWriter, r *http.Request, next htt
err = fmt.Errorf("%v", rec)
}
logger := LoggerWithTraceInfo(p.logger, r)
logger.Error("panic-check", slog.String("host", r.Host), log.ErrAttr(err), slog.Any("stacktrace", runtime.StartTrace()))
logger.Error("panic-check", slog.String("host", r.Host), log.ErrAttr(err), slog.String("stacktrace", string(debug.Stack())))

rw.Header().Set(router_http.CfRouterError, "unknown_failure")
rw.WriteHeader(http.StatusBadGateway)
Expand Down
4 changes: 4 additions & 0 deletions handlers/paniccheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"regexp"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -57,6 +58,9 @@ var _ = Describe("Paniccheck", func() {
Eventually(logger).Should(gbytes.Say("somehost.com"))
Eventually(logger).Should(gbytes.Say("we expect this panic"))
Eventually(logger).Should(gbytes.Say("stacktrace"))

// check that the stack trace is there and mentions the panicCheck handler.
Eventually(logger).Should(gbytes.Say(regexp.QuoteMeta("handlers.(*panicCheck)")))
})
})

Expand Down
6 changes: 5 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func initializeLogger() *slog.Logger {
conf.level,
)

zapHandler := zapslog.NewHandler(zapCore, zapslog.WithCaller(true))
// Disable adding the stack trace to all error messages automatically.
// The stack trace in the panic handler is added manually and remains in effect.
disableErrorStacktrace := zapslog.AddStacktraceAt(slog.LevelError + 1)

zapHandler := zapslog.NewHandler(zapCore, zapslog.WithCaller(true), disableErrorStacktrace)
slogFrontend := slog.New(zapHandler)
return slogFrontend
}
Expand Down
19 changes: 19 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ var _ = Describe("Logger", func() {
})
})

Describe("Error", func() {
Context("when an error is logged with 'Error' level", func() {
JustBeforeEach(func() {
logger = log.CreateLogger()
})
It("outputs an error log message without stack trace", func() {
Expect(func() { logger.Error(action) }).NotTo(Panic())

Expect(testSink.Lines()).To(HaveLen(1))
Expect(testSink.Lines()[0]).To(MatchRegexp(
`{"log_level":3,"timestamp":[0-9]+[.][0-9]+,"message":"%s"`,
action,
))
Expect(testSink.Lines()[0]).NotTo(ContainSubstring("stack_trace"))
Expect(testSink.Lines()[0]).NotTo(ContainSubstring("logger_test"))
})
})
})

Describe("ErrAttr", func() {
Context("when appending an error created by ErrAttr ", func() {
JustBeforeEach(func() {
Expand Down