-
-
Notifications
You must be signed in to change notification settings - Fork 998
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
Update logging example to use preview version of new log/slog #771
Conversation
Very nice! And thank you. Will review in detail soon |
I tried to keep the example as close to the previous as possible. Just learning the new library, too. I’ll be away for a few days, but will address anything when I get back home. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good :) please change the names back to as close to the example as possible
_examples/logging/main.go
Outdated
|
||
// Routes | ||
r := chi.NewRouter() | ||
r.Use(middleware.RequestID) | ||
r.Use(NewStructuredLogger(logger)) | ||
r.Use(NewStructuredLoggerMiddleware(slogJSONHandler)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please keep same name, NewStructuredLogger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
_examples/logging/main.go
Outdated
r.Use(middleware.Recoverer) | ||
|
||
r.Get("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("welcome")) | ||
}) | ||
r.Get("/wait", func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(1 * time.Second) | ||
LogEntrySetField(r, "wait", true) | ||
StructuredLogEntrySetField(r, "wait", true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use same name as before.. dont change the names please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
_examples/logging/main.go
Outdated
http.ListenAndServe(":3333", r) | ||
} | ||
|
||
// StructuredLogger is a simple, but powerful implementation of a custom structured | ||
// logger backed on logrus. I encourage users to copy it, adapt it and make it their | ||
// StructuredLogHandler is a simple, but powerful implementation of a custom structured |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name change..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
awesome work thank you :) |
Addresses #761 by replacing the logrus implementation with the preview version of the new
log/slog
package.Structured
to all three).StructuredLogEntrySetFields()
function that wasn't being called.Output of new logger for route: "/"
{"level":"INFO","msg":"request started","ts":"Sat, 17 Dec 2022 07:35:04 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000001","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53853","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/","ts":"Sat, 17 Dec 2022 07:35:04 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000001"}
{"level":"INFO","msg":"request complete","ts":"Sat, 17 Dec 2022 07:35:04 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000001","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53853","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/","resp_status":200,"resp_byte_length":7,"resp_elapsed_ms":0.012206}
Output of new logger for route: "/wait"
{"level":"INFO","msg":"request started","ts":"Sat, 17 Dec 2022 07:35:07 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000002","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53854","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/wait","ts":"Sat, 17 Dec 2022 07:35:07 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000002"}
{"level":"INFO","msg":"request complete","ts":"Sat, 17 Dec 2022 07:35:07 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000002","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53854","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/wait","wait":true,"resp_status":200,"resp_byte_length":2,"resp_elapsed_ms":1000.626244}
Output of new logger for route: "/panic"
{"level":"INFO","msg":"request started","ts":"Sat, 17 Dec 2022 07:35:13 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000003","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53856","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/panic","ts":"Sat, 17 Dec 2022 07:35:13 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000003"}
{"level":"INFO","msg":"","ts":"Sat, 17 Dec 2022 07:35:13 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000003","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53856","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/panic","stack":"goroutine 50 [running]:\nruntime/debug.Stack()\n\t/usr/local/opt/go/libexec/src/runtime/debug/stack.go:24 +0x65\ngithub.com/go-chi/chi/v5/middleware.Recoverer.func1.1()\n\t/Users/brady.briggs/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.8/middleware/recoverer.go:34 +0x99\npanic({0x12aee80, 0x1387b10})\n\t/usr/local/opt/go/libexec/src/runtime/panic.go:884 +0x212\nmain.main.func4({0x1305878?, 0x0?}, 0xc0000b00a0?)\n\t/Users/brady.briggs/Documents/development/github.com/ydarb/chi/_examples/logging/main.go:51 +0x27\nnet/http.HandlerFunc.ServeHTTP(0x12be280?, {0x29603418?, 0xc00009c1c0?}, 0xc0000a2004?)\n\t/usr/local/opt/go/libexec/src/net/http/server.go:2109 +0x2f\ngithub.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc0002122a0, {0x29603418, 0xc00009c1c0}, 0xc0000a0300)\n\t/Users/brady.briggs/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.8/mux.go:444 +0x216\nnet/http.HandlerFunc.ServeHTTP(0x100e767?, {0x29603418?, 0xc00009c1c0?}, 0x152d801?)\n\t/usr/local/opt/go/libexec/src/net/http/server.go:2109 +0x2f\ngithub.com/go-chi/chi/v5/middleware.Recoverer.func1({0x29603418?, 0xc00009c1c0?}, 0xc000092200?)\n\t/Users/brady.briggs/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.8/middleware/recoverer.go:43 +0x83\nnet/http.HandlerFunc.ServeHTTP(0xc0000a0200?, {0x29603418?, 0xc00009c1c0?}, 0x107cdbe?)\n\t/usr/local/opt/go/libexec/src/net/http/server.go:2109 +0x2f\ngithub.com/go-chi/chi/v5/middleware.RequestLogger.func1.1({0x138ae80, 0xc0000ac000}, 0xc0000a0200)\n\t/Users/brady.briggs/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.8/middleware/logger.go:54 +0x17d\nnet/http.HandlerFunc.ServeHTTP(0x138b3a0?, {0x138ae80?, 0xc0000ac000?}, 0x13874f0?)\n\t/usr/local/opt/go/libexec/src/net/http/server.go:2109 +0x2f\ngithub.com/go-chi/chi/v5/middleware.RequestID.func1({0x138ae80, 0xc0000ac000}, 0xc0000a0100)\n\t/Users/brady.briggs/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.8/middleware/request_id.go:76 +0x22d\nnet/http.HandlerFunc.ServeHTTP(0x138b2f8?, {0x138ae80?, 0xc0000ac000?}, 0x152d7f0?)\n\t/usr/local/opt/go/libexec/src/net/http/server.go:2109 +0x2f\ngithub.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc0002122a0, {0x138ae80, 0xc0000ac000}, 0xc0000a0000)\n\t/Users/brady.briggs/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.8/mux.go:90 +0x310\nnet/http.serverHandler.ServeHTTP({0xc000092090?}, {0x138ae80, 0xc0000ac000}, 0xc0000a0000)\n\t/usr/local/opt/go/libexec/src/net/http/server.go:2947 +0x30c\nnet/http.(*conn).serve(0xc000096000, {0x138b3a0, 0xc000203560})\n\t/usr/local/opt/go/libexec/src/net/http/server.go:1991 +0x607\ncreated by net/http.(*Server).Serve\n\t/usr/local/opt/go/libexec/src/net/http/server.go:3102 +0x4db\n","panic":"oops"}
Output of new logger for route: "/add_fields"
{"level":"INFO","msg":"request complete","ts":"Sat, 17 Dec 2022 07:35:13 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000003","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53856","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/panic","resp_status":500,"resp_byte_length":0,"resp_elapsed_ms":2.039354}
{"level":"INFO","msg":"request started","ts":"Sat, 17 Dec 2022 07:35:21 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000004","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53857","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/add_fields","ts":"Sat, 17 Dec 2022 07:35:21 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000004"}
{"level":"INFO","msg":"request complete","ts":"Sat, 17 Dec 2022 07:35:21 UTC","req_id":"bbriggs-mac.local/IDKVRVdgFj-000004","http_scheme":"http","http_proto":"HTTP/1.1","http_method":"GET","remote_addr":"127.0.0.1:53857","user_agent":"curl/7.84.0","uri":"http://127.0.0.1:3333/add_fields","foo":"bar","bar":"foo","resp_status":0,"resp_byte_length":0,"resp_elapsed_ms":0.021186}