{"id":22511004,"url":"https://github.com/marnixbouhuis/zaphttp","last_synced_at":"2026-05-18T04:04:28.475Z","repository":{"id":266877060,"uuid":"899618685","full_name":"marnixbouhuis/zaphttp","owner":"marnixbouhuis","description":"HTTP request logger for Go and Zap","archived":false,"fork":false,"pushed_at":"2025-12-15T22:22:16.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-19T06:32:41.028Z","etag":null,"topics":["golang","golang-library","http","logging","opentelemetry","opentelemetry-go","zap"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marnixbouhuis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2024-12-06T16:36:39.000Z","updated_at":"2025-12-15T22:20:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"89afaca1-c1f0-4733-89dc-b83006a71c7e","html_url":"https://github.com/marnixbouhuis/zaphttp","commit_stats":null,"previous_names":["marnixbouhuis/zaphttp"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/marnixbouhuis/zaphttp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marnixbouhuis%2Fzaphttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marnixbouhuis%2Fzaphttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marnixbouhuis%2Fzaphttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marnixbouhuis%2Fzaphttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marnixbouhuis","download_url":"https://codeload.github.com/marnixbouhuis/zaphttp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marnixbouhuis%2Fzaphttp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33164672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T22:39:12.733Z","status":"online","status_checked_at":"2026-05-18T02:00:06.436Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["golang","golang-library","http","logging","opentelemetry","opentelemetry-go","zap"],"created_at":"2024-12-07T02:07:48.733Z","updated_at":"2026-05-18T04:04:28.470Z","avatar_url":"https://github.com/marnixbouhuis.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zaphttp - Structured Logging for HTTP Requests in Go\n[![Go Reference](https://pkg.go.dev/badge/github.com/marnixbouhuis/zaphttp.svg)](https://pkg.go.dev/github.com/marnixbouhuis/zaphttp)\n[![CI/CD Pipeline](https://github.com/marnixbouhuis/zaphttp/actions/workflows/cicd.yaml/badge.svg)](https://github.com/marnixbouhuis/zaphttp/actions/workflows/cicd.yaml)\n\nzaphttp is a Go library that provides structured logging for HTTP requests using [zap](https://github.com/uber-go/zap). It creates a per-request logger that automatically has things like a trace ID injected into it.\n\n## Features\n\n- Per-request logger injection\n- OpenTelemetry integration\n- Flexible log formatting\n- Built-in support for formats like Elastic Common Schema (ECS) and Google Cloud logging\n- Customizable per-request logger behavior\n\n## Installation\n\n```bash\ngo get github.com/marnixbouhuis/zaphttp\n```\n\n## Quick Start\n\n```go\nlogger := zap.NewExample()\nzap.ReplaceGlobals(logger)\ndefer func() {\n\t_ = logger.Sync()\n}()\n\nmux := http.NewServeMux()\nmux.HandleFunc(\"/demo/{$}\", func(w http.ResponseWriter, req *http.Request) {\n\t// Optional, get the logger for this request from the context.\n\t// If you are using opentelemetry, the trace ID is automatically injected into each log message.\n\tl := zaphttp.FromContext(req.Context())\n\n\t// Optional, log something with the request logger.\n\tl.Info(\"Some message!\")\n\n\tw.Header().Set(\"Content-Type\", \"text/plain\")\n\tw.WriteHeader(http.StatusOK)\n\t_, _ = w.Write([]byte(\"Hello world!\"))\n})\n\nrequestLogger := zaphttp.NewHandler(\n\tzaphttp.WithLogger(logger),                                         // If no logger is supplied, zap.L(), will be used.\n\tzaphttp.WithTraceFormatter(zaphttp.ElasticCommonSchemaFormatter),   // If no format for trace metadata is supplied, ECS is used.\n\tzaphttp.WithRequestFormatter(zaphttp.ElasticCommonSchemaFormatter), // If no format for request metadata is supplied, ECS is used.\n)\n\ns := \u0026http.Server{\n\tAddr:         \":8080\",\n\tReadTimeout:  5 * time.Second,\n\tWriteTimeout: 5 * time.Second,\n\tHandler:      requestLogger(mux), // Wrap the mux, all requests will now be logged.\n}\n\n// Do graceful shutdown of HTTP server here...\n\nif err := s.ListenAndServe(); err != nil \u0026\u0026 !errors.Is(err, http.ErrServerClosed) {\n\tlogger.Error(\"Failed to start server\", zap.Error(err))\n}\n```\n\nEvery request received by the HTTP server is now logged.\n\n## Core Concepts\n\n### Handler\nThe main component is the `NewHandler()` function that creates a middleware to wrap your HTTP handlers.\n\nOptions:\n- `WithLogger(logger *zap.Logger)` - Set a custom logger (default: `zap.L()`)\n- `WithTraceFormatter(formatter TraceFormatter)` - Set a custom trace formatter (default: ECS)\n- `WithRequestFormatter(formatter RequestFormatter)` - Set a custom request formatter (default: ECS)\n- `WithPerRequestLogger(fn PerRequestLoggerFunc)` - Customize how the per-request logger is created\n- `WithPerRequestFilter(fn PerRequestFilterFunc)` - Customize which requests should be logged (default: all requests)\n\n### Formatters\nFormatters determine how request and trace information is structured in the logs.\n\nBuilt-in formatters:\n- `ElasticCommonSchemaFormatter` - Formats logs according to the Elastic Common Schema\n- `NewGoogleCloudFormatter(projectID)` - Formats logs for Google Cloud Logging\n- `NoopFormatter` - Disables all extra fields\n\n### Per-Request Logger\nThe per-request logger is injected into the request context and can be retrieved using `FromContext()`. It automatically includes:\n\n- Trace ID (if using OpenTelemetry)\n- Custom fields added by the per-request logger function\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarnixbouhuis%2Fzaphttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarnixbouhuis%2Fzaphttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarnixbouhuis%2Fzaphttp/lists"}