-
-
Notifications
You must be signed in to change notification settings - Fork 988
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's LogAttrs (and other methods) now requires a context as their first argument #858
Comments
mhghw
changed the title
log/slog now requires a context as their first argument
log/slog's LogAttrs (and other methods) now requires a context as their first argument
Oct 17, 2023
I just want to add that slogJSONHandler := slog.HandlerOptions{
// Remove default time slog.Attr, we create our own later
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}.NewJSONHandler(os.Stdout) is no longer valid. The following is the right syntax slogJSONHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
// Remove default time slog.Attr, we create our own later
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}) |
ydarb
added a commit
to ydarb/chi
that referenced
this issue
Oct 19, 2023
chore: Fix logging example Panic() method to pretty print stack trace Ref: go-chi#858
ydarb
added a commit
to ydarb/chi
that referenced
this issue
Oct 19, 2023
Adds bool to flip example between a TextHandler and a JSONHandler. Update Panic() to pretty print stack trace if log handler is a TextHandler, otherwise it will log the stack trace as a JSON value string with a key of "stack". Ref: go-chi#858
https://github.com/go-chi/httplog/releases/tag/v2.0.0 is out built on "log/slog" and has lots of little nice features including a pretty logger when printing to stdout tty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I was looking around the examples and in the logging example I found this struct that satisfies the
middleware.LogEntry
interface:Since its using
log/slog
as its logger, the LogAttrs method for thelog/slog
is changed fromfunc (l *Logger) LogAttrs(level Level, msg string, attrs ...Attr)
to
func (l *Logger) LogAttrs(ctx context.Context,level Level, msg string, attrs ...Attr)
and well, your example is not working anymore.
I read the source code and I couldn't figure out why there is a context passing around but it seemed that we can pass context.Background() to the
LogAttr
method (or even nil) andlog/slog
package handles it.Can you please clarify the usage of context in here for me and see if it's possible to change the
middleware.LogEntry
interface that we could send the request context as an argument to use inlog/slog
methods.Thank you very much.
The text was updated successfully, but these errors were encountered: