{"id":19938106,"url":"https://github.com/dweinstein/go-http-logger-example","last_synced_at":"2026-05-12T11:34:15.134Z","repository":{"id":142071233,"uuid":"76129514","full_name":"dweinstein/go-http-logger-example","owner":"dweinstein","description":"Logging the status code for an HTTP response in Go turns out to be a little tricker than first anticipated. You don't get access to the `Response` object after your handler, rather you only have the `ResponseWriter` which is like a byte stream.","archived":false,"fork":false,"pushed_at":"2016-12-10T18:22:40.000Z","size":2,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-01T12:42:12.196Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dweinstein.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-10T18:16:26.000Z","updated_at":"2018-08-25T23:28:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"fa89cba3-5167-4a58-bbd7-ea4e0ec80104","html_url":"https://github.com/dweinstein/go-http-logger-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dweinstein/go-http-logger-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweinstein%2Fgo-http-logger-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweinstein%2Fgo-http-logger-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweinstein%2Fgo-http-logger-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweinstein%2Fgo-http-logger-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dweinstein","download_url":"https://codeload.github.com/dweinstein/go-http-logger-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweinstein%2Fgo-http-logger-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32937442,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-12T09:19:52.626Z","status":"ssl_error","status_checked_at":"2026-05-12T09:17:33.438Z","response_time":102,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T23:38:22.903Z","updated_at":"2026-05-12T11:34:15.102Z","avatar_url":"https://github.com/dweinstein.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SYNOPSIS\n\ngo http logging - some opinion how to log http requests/responses\n\nThis isn't a library or anything fancy, just a mini tutorial on getting at the http\nresponse code...\n\n# DESCRIPTION\n\nLogging the status code for an HTTP response in Go turns out to be a little\ntricker than first anticipated. You don't get access to the `Response` object\nafter your handler, rather you only have the `ResponseWriter` which is like a\nbyte stream.\n\nSo I ripped out the `responseLogger` from Gorilla's\n[handlers](https://github.com/gorilla/handlers) package and made it so that I\ncan get the response status code and the response size after our handler runs.\n\nAlso I went with a structured logger\n[logrus](https://github.com/sirupsen/logrus) as that's closer to what I'm used\nto using in node land, e.g., [bunyan](https://github.com/trentm/node-bunyan).\n\n# EXAMPLE\n\n## Server\n```sh\n± go run main.go response_logger.go\n{\"level\":\"info\",\"msg\":\"listening on port 9990\",\"time\":\"2016-12-10T13:20:35-05:00\"}\n{\"addr\":\"[::1]:49290\",\"code\":200,\"level\":\"info\",\"method\":\"GET\",\"msg\":\"HTTP\",\"size\":14,\"time\":\"2016-12-10T13:20:38-05:00\",\"took\":\"24.466µs\",\"url\":\"/\"}\n{\"addr\":\"[::1]:49297\",\"code\":404,\"level\":\"info\",\"method\":\"GET\",\"msg\":\"HTTP\",\"size\":15,\"time\":\"2016-12-10T13:20:40-05:00\",\"took\":\"9.058µs\",\"url\":\"/status\"}\n```\n\n## Client\n\n```sh\n± curl -v -H'Accept: application/json' localhost:9990/\n*   Trying ::1...\n* Connected to localhost (::1) port 9990 (#0)\n\u003e GET / HTTP/1.1\n\u003e Host: localhost:9990\n\u003e User-Agent: curl/7.49.1\n\u003e Accept: application/json\n\u003e\n\u003c HTTP/1.1 200 OK\n\u003c Date: Sat, 10 Dec 2016 18:21:22 GMT\n\u003c Content-Length: 14\n\u003c Content-Type: text/plain; charset=utf-8\n\u003c\n* Connection #0 to host localhost left intact\nUp and running%\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdweinstein%2Fgo-http-logger-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdweinstein%2Fgo-http-logger-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdweinstein%2Fgo-http-logger-example/lists"}