{"id":18656612,"url":"https://github.com/zendesk/go-httpclerk","last_synced_at":"2025-04-11T18:30:58.657Z","repository":{"id":18985657,"uuid":"22207138","full_name":"zendesk/go-httpclerk","owner":"zendesk","description":"A simple HTTP request/response logger for Go supporting multiple formatters.","archived":false,"fork":false,"pushed_at":"2014-08-07T07:45:45.000Z","size":282,"stargazers_count":50,"open_issues_count":1,"forks_count":2,"subscribers_count":426,"default_branch":"master","last_synced_at":"2025-03-25T16:51:41.081Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://godoc.org/github.com/zendesk/go-httpclerk","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zendesk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-24T09:25:30.000Z","updated_at":"2024-11-29T11:44:34.000Z","dependencies_parsed_at":"2022-08-05T02:01:59.676Z","dependency_job_id":null,"html_url":"https://github.com/zendesk/go-httpclerk","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fgo-httpclerk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fgo-httpclerk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fgo-httpclerk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fgo-httpclerk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zendesk","download_url":"https://codeload.github.com/zendesk/go-httpclerk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248458376,"owners_count":21107064,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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-07T07:24:20.877Z","updated_at":"2025-04-11T18:30:58.332Z","avatar_url":"https://github.com/zendesk.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"go-httpclerk\n==============\n\n## Overview\n\nA simple HTTP request/response logger for Go supporting multiple formatters.\n\n## Rationale\n\nWe needed a way to log HTTP requests at Zendesk to different log backends (stdout, syslog etc.) with multiple ways to format them (including logstash). So we created this project to help us.\n\n## Usage\n\nYou'll need to create some sort of logger that conforms to the `LogDestination` interface in this package. The [go-logger](https://github.com/op/go-logging) package is recommended.\n\n### Simple example:\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/op/go-logging\"\n\t\"github.com/zendesk/go-httpclerk\"\n\tstdlog \"log\"\n\t\"net/http\"\n\t\"os\"\n)\n\nvar log = logging.MustGetLogger(\"myApp\")\n\nfunc main() {\n\t// Setup a go-logging logger\n\tstdoutBackend := logging.NewLogBackend(os.Stderr, \"\", stdlog.LstdFlags|stdlog.Lshortfile)\n\tlogging.SetBackend(stdoutBackend) // See go-logging docs for multiple backends\n\tlogging.SetLevel(logging.DEBUG, \"myApp\")\n\n\t// Boot web server and listen on 8080\n\thttp.HandleFunc(\"/\", handler)\n\thttp.ListenAndServe(\":8080\", nil)\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\tformatter, _ := httpclerk.NewTextFormatter(\"myHandler\")\n\tclerk, err := httpclerk.NewHTTPLogger(\"myHandler\", log, formatter)\n\tif err != nil {\n\t\tlog.Fatal(\"HTTP logger could not be created\", err)\n\t}\n\tdefer clerk.Info(w, r)\n\n\tfmt.Fprintf(w, \"Hi there, I love %s!\", r.URL.Path[1:])\n}\n```\n\nThis will produce logs like so:\n\n```\n2014/07/27 07:43:56 http_logger.go:39: myHandler 1974-carcher.local \u003e Method: GET Path: /ciaran Status:  Host: localhost:8080 Headers: map[User-Agent:[curl/7.30.0] Accept:[*/*]]\n```\n\n### Status Code\n\nYou'll notice that the `Status` is blank. This is becuase there is no simple way to get the response status in a HTTP handler without wrapping the `ResponseWriter` type. You can see an example of this [here](https://gist.github.com/ciaranarcher/abccf50cb37645ca27fa). If you do this, and use this type instead of the standard `ResponseWriter` then the `go-httpclerk` package can fetch the status code and include it in logging:\n\n```\n2014/07/27 07:43:56 http_logger.go:39: myHandler 1974-carcher.local \u003e Method: GET Path: /ciaran Status: 200 Host: localhost:8080 Headers: map[User-Agent:[curl/7.30.0] Accept:[*/*]]\n```\n\n### Other Formatters\n\nIncluded in the package is a `TextFormatter` (examples above use this) and a `LogStashFormatter` for JSON logging\n\n```\nformatter, _ := NewLogStashFormatter(\"fooApp\", []string{\"blimp\", \"foo\"})\n```\n\nOther loggers can be used in place if they implement the the following interface:\n\n```\ntype Formatter interface {\n\tFormat(interface{}) (string, error)\n}\n```\n\n## Contributing\n\nCreate a Pull Request with your changes, ping someone and we'll look at getting it merged.\n\n## Copyright and license\n\nCopyright 2013 Zendesk\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License.\n\nYou may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fgo-httpclerk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzendesk%2Fgo-httpclerk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fgo-httpclerk/lists"}