From b4f18384b1fed45f25c43b1e41b809271653ca6b Mon Sep 17 00:00:00 2001 From: Kyle VanderBeek Date: Thu, 3 Oct 2024 18:25:19 -0700 Subject: [PATCH] Use httptest.Server to fully demonstrate hlog. It is able to run all the handlers; the former implementation didn't. --- hlog/hlog_example_test.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/hlog/hlog_example_test.go b/hlog/hlog_example_test.go index c744249b..89b4f72b 100644 --- a/hlog/hlog_example_test.go +++ b/hlog/hlog_example_test.go @@ -50,24 +50,31 @@ func Example_handler() { // Install some provided extra handlers to set some request's context fields. // Thanks to those handlers, all our logs will come with some pre-populated fields. - c = c.Append(hlog.RemoteAddrHandler("ip")) + c = c.Append(hlog.HTTPVersionHandler("http_version")) c = c.Append(hlog.UserAgentHandler("user_agent")) - c = c.Append(hlog.RefererHandler("referer")) + c = c.Append(hlog.URLHandler("url")) //c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id")) // Here is your final handler h := c.Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + status := http.StatusOK // Your business logic here + // Get the logger from the request's context. You can safely assume it // will be always there: if the handler is removed, hlog.FromRequest // will return a no-op logger. hlog.FromRequest(r).Info(). Str("user", "current user"). - Str("status", "ok"). + Int("status", status). Msg("Something happened") })) - http.Handle("/", h) - h.ServeHTTP(httptest.NewRecorder(), &http.Request{}) + ts := httptest.NewServer(h) + defer ts.Close() + + _, err := http.Get(ts.URL) + if err != nil { + panic("http.Get failed") + } - // Output: {"level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","time":"2001-02-03T04:05:06Z","message":"Something happened"} + // Output: {"level":"info","role":"my-service","host":"local-hostname","http_version":"1.1","user_agent":"Go-http-client/1.1","url":"/","user":"current user","status":200,"time":"2001-02-03T04:05:06Z","message":"Something happened"} }