{"id":13830425,"url":"https://github.com/FabienMht/ginslog","last_synced_at":"2025-07-09T11:33:04.537Z","repository":{"id":192916257,"uuid":"686569508","full_name":"FabienMht/ginslog","owner":"FabienMht","description":"A fully featured Gin middlewares for slog logging","archived":false,"fork":false,"pushed_at":"2023-09-20T21:19:22.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-04T10:03:05.847Z","etag":null,"topics":[],"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/FabienMht.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-09-03T08:26:25.000Z","updated_at":"2023-12-06T09:55:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"56b4dcbc-86db-457f-93c0-63b2167f0f03","html_url":"https://github.com/FabienMht/ginslog","commit_stats":null,"previous_names":["fabienmht/ginslog"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FabienMht%2Fginslog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FabienMht%2Fginslog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FabienMht%2Fginslog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FabienMht%2Fginslog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FabienMht","download_url":"https://codeload.github.com/FabienMht/ginslog/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225539392,"owners_count":17485318,"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-08-04T10:00:59.777Z","updated_at":"2024-11-20T11:30:45.599Z","avatar_url":"https://github.com/FabienMht.png","language":"Go","funding_links":[],"categories":["Integrations"],"sub_categories":[],"readme":"# ginslog\n\n![Go Version](https://img.shields.io/github/go-mod/go-version/FabienMht/ginslog.svg)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://pkg.go.dev/github.com/FabienMht/ginslog)\n[![Go Report Card](https://goreportcard.com/badge/github.com/FabienMht/ginslog)](https://goreportcard.com/report/github.com/FabienMht/ginslog)\n[![Sourcegraph](https://sourcegraph.com/github.com/FabienMht/ginslog/-/badge.svg)](https://sourcegraph.com/github.com/FabienMht/ginslog)\n[![Tag](https://img.shields.io/github/tag/FabienMht/ginslog.svg)](https://github.com/FabienMht/ginslog/tags)\n[![Contributors](https://img.shields.io/github/contributors/FabienMht/ginslog)](https://github.com/FabienMht/ginslog/graphs/contributors)\n[![License](https://img.shields.io/github/license/FabienMht/ginslog)](./LICENSE)\n\nA fully featured Gin middlewares for slog logging.\nIt includes a logging and a panic recovery middlewares.\n\n## Install\n\nCompatibility: go \u003e= 1.21\n\n**Download it:**\n\n```\ngo get github.com/FabienMht/ginslog\n```\n\n**Add the following import:**\n\n```go\nginlogger \"github.com/FabienMht/ginslog/logger\"\nginrecovery \"github.com/FabienMht/ginslog/recovery\"\n```\n\n## Usage\n\n### Basic text handler\n\n```go\npackage main\n\nimport (\n    \"log/slog\"\n\n    ginlogger \"github.com/FabienMht/ginslog/logger\"\n    ginrecovery \"github.com/FabienMht/ginslog/recovery\"\n    \"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n    // Create a new logger instance with a text handler.\n    logger := slog.New(slog.NewTextHandler(os.Stderr, nil))\n\n    r := gin.New()\n    // Add the ginslog middleware to all routes.\n    // Default attributes are:\n    // - time\n    // - level\n    // - msg\n    // - ip\n    // - status\n    // - method\n    // - path\n    // - user-agent\n    // - latency\n    // - request-id\n    r.Use(ginlogger.New(logger))\n    r.Use(ginrecovery.New(logger))\n\n    r.GET(\"/test\", func(c *gin.Context) {\n        c.String(200, \"Hello world!\")\n    })\n\n    r.GET(\"/panic\", func(c *gin.Context) {\n        panic(\"Panic!\")\n    })\n\n    r.Run(\":80\")\n}\n```\n\n**Output:**\n\n```bash\n# Log incoming request\n$ curl 127.0.0.1:80/test\ntime=2023-01-01T00:00:00.000+02:00 level=INFO msg=\"Incoming request\" ip=\"127.0.0.1\" status=200 method=GET path=/test user-agent=curl/7.86.0 latency=13.877µs request-id=52fdfc07-2182-454f-963f-5f0f9a621d72\n# Log panic recovered with stack trace and request\n$ curl 127.0.0.1:80/panic\ntime=2023-01-01T00:00:00.000+02:00 level=ERROR msg=\"Panic recovered\" error=\"Unexpected error\" request=\"GET /panic HTTP/1.1\\r\\nHost: 127.0.0.1:8080\\r\\nAccept: */*\\r\\nUser-Agent: curl/7.86.0\\r\\n\\r\\n\" stack=\"goroutine 19 [running]:\\nruntime/debug.Stack()\\n\\t/usr/lib/go/src/runtime/debug/stack.go:24 +0x5e\\n...\\ncreated by net/http.(*Server).Serve in goroutine 1\\n\\t/usr/lib/go/src/net/http/server.go:3086 +0x5cb\\n\"\ntime=2023-01-01T00:00:00.000+02:00 level=ERROR msg=\"Incoming request\" ip=127.0.0.1 status=500 method=GET path=/panic user-agent=curl/7.86.0 latency=220.331µs\n```\n\n### Basic JSON handler\n\n```go\npackage main\n\nimport (\n    \"log/slog\"\n\n    ginlogger \"github.com/FabienMht/ginslog/logger\"\n    ginrecovery \"github.com/FabienMht/ginslog/recovery\"\n    \"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n    // Create a new logger instance with a JSON handler.\n    logger := slog.New(slog.NewJSONHandler(os.Stderr, nil))\n\n    r := gin.New()\n\n    // Add the ginslog middleware to all routes.\n    // Default attributes are:\n    // - time\n    // - level\n    // - msg\n    // - ip\n    // - status\n    // - method\n    // - path\n    // - user-agent\n    // - latency\n    // - request-id\n    r.Use(ginlogger.New(logger))\n    r.Use(ginrecovery.New(logger))\n\n    r.GET(\"/test\", func(c *gin.Context) {\n        c.String(200, \"Hello world!\")\n    })\n\n    r.GET(\"/panic\", func(c *gin.Context) {\n        panic(\"Panic!\")\n    })\n\n    r.Run(\":80\")\n}\n```\n\n**Output:**\n\n```bash\n# Log incoming request\n$ curl 127.0.0.1:80/test\n{\"time\":\"2023-01-01T00:00:00.000+02:00\",\"level\":\"INFO\",\"msg\":\"Incoming request\",\"ip\":\"127.0.0.1\",\"status\":200,\"method\":\"GET\",\"path\":\"/test\",\"user-agent\":\"curl/7.86.0\",\"latency\":43750,\"request-id\":\"52fdfc07-2182-454f-963f-5f0f9a621d72\"}\n# Log panic recovered with stack trace and request\n$ curl 127.0.0.1:80/panic\n{\"time\":\"2023-01-01T00:00:00.000+02:00\",\"level\":\"ERROR\",\"msg\":\"Panic recovered\",\"error\":\"Unexpected error\",\"request\":\"GET /panic HTTP/1.1\\r\\nHost: 127.0.0.1:8080\\r\\nAccept: */*\\r\\nUser-Agent: curl/7.86.0\\r\\n\\r\\n\",\"stack\":\"goroutine 6 [running]:\\nruntime/debug.Stack()\\n\\t/usr/lib/go/src/runtime/debug/stack.go:24 +0x5e\\n...\\ncreated by net/http.(*Server).Serve in goroutine 1\\n\\t/usr/lib/go/src/net/http/server.go:3086 +0x5cb\\n\"}\n{\"time\":\"2023-01-01T00:00:00.000+02:00\",\"level\":\"ERROR\",\"msg\":\"Incoming request\",\"ip\":\"127.0.0.1\",\"status\":500,\"method\":\"GET\",\"path\":\"/panic\",\"user-agent\":\"curl/7.86.0\",\"latency\":209845,\"request-id\":\"52fdfc07-2182-454f-963f-5f0f9a621d72\"}\n```\n\n## Contributing\n\nContributions are welcome ! Please open an issue or submit a pull request.\n\n```bash\n# Install task\n$ go install github.com/go-task/task/v3/cmd/task@v3\n\n# Install dev dependencies\n$ task dev\n\n# Run linter\n$ task lint\n\n# Run tests\n$ task test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFabienMht%2Fginslog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFabienMht%2Fginslog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFabienMht%2Fginslog/lists"}