-
Notifications
You must be signed in to change notification settings - Fork 18k
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
log/slog: Using SetDefault
causes level
and msg
being rendered wrong if log called in a panic recovery middleware
#64015
Labels
FrozenDueToAge
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
Comments
Please include a complete reproducer. package main
import (
"log/slog"
"net/http"
"os"
)
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil))) // Or use NewTextHandler
http.Handle("/", recovery(http.HandlerFunc(handler)))
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
slog.InfoContext(r.Context(), "1")
slog.WarnContext(r.Context(), "2")
slog.ErrorContext(r.Context(), "3")
panic("test")
}
func recovery(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
slog.ErrorContext(r.Context(), "4")
w.WriteHeader(http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
} |
Sorry, it has to be related to something I have in the app. I've tried again and it seems to be working fine. |
Found it. When importing |
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 11, 2024
Use the slog experimental version to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 15, 2024
Use the slog experimental version to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 15, 2024
Use the slog experimental version to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 15, 2024
Use the log/slog package to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 16, 2024
Use the log/slog package to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 23, 2024
Use the log/slog package to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to avisiedo/idmsvc-backend
that referenced
this issue
Jul 25, 2024
Use the log/slog package to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
avisiedo
added a commit
to podengo-project/idmsvc-backend
that referenced
this issue
Jul 25, 2024
Use the log/slog package to keep the consistency cross the repository. Use different packages could evoke the situation described at: golang/go#64015 Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
FrozenDueToAge
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
After sending a test request to HTTP endpoint to simulate panic and recovery, last log looks wrong.
Note: If you also use a custom log handler that extracts and adds custom fields to logs, none of those fields appear in logs either.
What did you see instead?
When used
NewJSONHandler
.When used
NewTextHandler
.As you can see above, the last log entry is not right!
If you omit
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
frommain
function, output seems correct for text version.The text was updated successfully, but these errors were encountered: