{"id":50771416,"url":"https://github.com/tinh-tinh/tslog","last_synced_at":"2026-06-11T19:01:33.757Z","repository":{"id":357164830,"uuid":"1185398519","full_name":"tinh-tinh/tslog","owner":"tinh-tinh","description":"slog for TinhTinh Framework","archived":false,"fork":false,"pushed_at":"2026-05-11T15:18:55.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-05-11T17:17:40.782Z","etag":null,"topics":["framework","golang","slog"],"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/tinh-tinh.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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-18T14:42:56.000Z","updated_at":"2026-05-11T15:33:59.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/tinh-tinh/tslog","commit_stats":null,"previous_names":["tinh-tinh/tslog"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/tinh-tinh/tslog","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Ftslog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Ftslog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Ftslog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Ftslog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tinh-tinh","download_url":"https://codeload.github.com/tinh-tinh/tslog/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Ftslog/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34213179,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"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":["framework","golang","slog"],"created_at":"2026-06-11T19:01:32.711Z","updated_at":"2026-06-11T19:01:33.750Z","avatar_url":"https://github.com/tinh-tinh.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# T-SLOG\n\n[![Go](https://github.com/tinh-tinh/tslog/actions/workflows/go.yml/badge.svg)](https://github.com/tinh-tinh/tslog/actions/workflows/go.yml)\n[![codecov](https://codecov.io/gh/tinh-tinh/tslog/graph/badge.svg?token=YOUR_TOKEN)](https://codecov.io/gh/tinh-tinh/tslog)\n[![Go Reference](https://pkg.go.dev/badge/github.com/tinh-tinh/tslog.svg)](https://pkg.go.dev/github.com/tinh-tinh/tslog)\n[![Go Report Card](https://goreportcard.com/badge/github.com/tinh-tinh/tslog)](https://goreportcard.com/report/github.com/tinh-tinh/tslog)\n![GitHub License](https://img.shields.io/github/license/tinh-tinh/tslog)\n\n**tslog** is a structured logging module for the [Tinh Tinh](https://github.com/tinh-tinh/tinhtinh) framework, built on top of Go's standard [`log/slog`](https://pkg.go.dev/log/slog) package. It integrates `slog` as a first-class provider within the Tinh Tinh dependency injection system and exposes a flexible middleware for HTTP request logging.\n\n---\n\n## Features\n\n- 🔌 **DI-friendly** — Register any `slog.Handler` as a module-level provider using `ForRoot`\n- 💉 **Easy injection** — Retrieve the logger anywhere via `Inject(module)`\n- 🛣️ **HTTP middleware** — Log every incoming request with a customisable function\n- ⏭️ **Skip paths** — Exclude health-check or any other routes from logging\n- 🔗 **Context-aware** — Use `slog.Logger.InfoContext` together with request context for trace/request-ID propagation\n\n---\n\n## Requirements\n\n| Dependency | Version |\n|---|---|\n| Go | ≥ 1.24 |\n| `github.com/tinh-tinh/tinhtinh` | v2 |\n\n---\n\n## Installation\n\n```bash\ngo get github.com/tinh-tinh/tslog\n```\n\n---\n\n## Quick Start\n\n### 1. Register the module\n\nPass any standard `slog.Handler` to `ForRoot`. The logger is registered as a named provider (`TSLOG`) and made available across all imported modules.\n\n```go\npackage main\n\nimport (\n    \"log/slog\"\n    \"os\"\n\n    \"github.com/tinh-tinh/tinhtinh/v2/core\"\n    \"github.com/tinh-tinh/tslog\"\n)\n\nfunc main() {\n    appModule := func() core.Module {\n        return core.NewModule(core.NewModuleOptions{\n            Imports: []core.Modules{\n                tslog.ForRoot(slog.NewJSONHandler(os.Stdout, nil)),\n            },\n            Controllers: []core.Controllers{userController},\n        })\n    }\n\n    app := core.CreateFactory(appModule)\n    app.Listen(3000)\n}\n```\n\n### 2. Inject and use the logger\n\nInside any controller or service, call `tslog.Inject(module)` to obtain the `*slog.Logger`.\n\n```go\nfunc userController(module core.Module) core.Controller {\n    ctrl := module.NewController(\"users\")\n    logger := tslog.Inject(module)\n\n    ctrl.Get(\"\", func(ctx core.Ctx) error {\n        logger.Info(\"Request processed\",\n            \"http\", slog.Group(\"request\",\n                \"method\", \"GET\",\n                \"path\", \"/api/users\",\n                \"status\", 200,\n            ),\n            \"duration_ms\", 42,\n        )\n        return ctx.JSON(map[string]any{\"ok\": true})\n    })\n\n    return ctrl\n}\n```\n\n---\n\n## HTTP Middleware\n\n### Basic logging middleware\n\n`tslog.Middleware` wraps a user-supplied function (`Fnc`) that receives the request context. Use it to log method, path, latency, or any attribute you need.\n\n```go\nloggerMiddleware := tslog.Middleware(tslog.MiddlewareOptions{\n    Fnc: func(ctx core.Ctx) {\n        logger := ctx.Ref(tslog.TSLOG).(*slog.Logger)\n        logger.Info(\"Incoming request\",\n            slog.Group(\"http\",\n                slog.Group(\"request\",\n                    \"method\", ctx.Req().Method,\n                    \"path\",   ctx.Req().URL.Path,\n                ),\n            ),\n        )\n    },\n})\n\nappModule := func() core.Module {\n    return core.NewModule(core.NewModuleOptions{\n        Imports:     []core.Modules{tslog.ForRoot(slog.NewJSONHandler(os.Stdout, nil))},\n        Controllers: []core.Controllers{userController},\n        Middlewares: []core.Middleware{loggerMiddleware},\n    })\n}\n```\n\n### Skipping paths\n\nSupply `SkipPaths` to prevent certain routes (e.g., health-check endpoints) from being logged.\n\n```go\nloggerMiddleware := tslog.Middleware(tslog.MiddlewareOptions{\n    SkipPaths: []string{\"/health\", \"/readyz\"},\n    Fnc: func(ctx core.Ctx) {\n        logger := ctx.Ref(tslog.TSLOG).(*slog.Logger)\n        logger.Info(\"Incoming request\",\n            \"method\", ctx.Req().Method,\n            \"path\",   ctx.Req().URL.Path,\n        )\n    },\n})\n```\n\n---\n\n## Context-Aware Logging (Trace / Request ID)\n\nCombine tslog with a custom `slog.Handler` that reads values from the request context to automatically attach trace or request IDs to every log line.\n\n```go\nconst reqIDKey = \"req_id\"\n\n// ContextHandler enriches each log record with the request ID stored in ctx.\ntype ContextHandler struct{ slog.Handler }\n\nfunc (h ContextHandler) Handle(ctx context.Context, r slog.Record) error {\n    if reqID, ok := ctx.Value(reqIDKey).(string); ok {\n        r.AddAttrs(slog.String(\"req_id\", reqID))\n    }\n    return h.Handler.Handle(ctx, r)\n}\n\n// traceMiddleware assigns a unique request ID and stores it in context.\ntraceMiddleware := func(ctx core.Ctx) error {\n    ctx.Set(reqIDKey, rand.Text())\n    return ctx.Next()\n}\n\n// loggerMiddleware logs using the enriched context.\nloggerMiddleware := tslog.Middleware(tslog.MiddlewareOptions{\n    Fnc: func(ctx core.Ctx) {\n        logger := ctx.Ref(tslog.TSLOG).(*slog.Logger)\n        logger.InfoContext(ctx.Req().Context(), \"API Request\")\n    },\n})\n\nappModule := func() core.Module {\n    base := slog.NewJSONHandler(os.Stdout, nil)\n    return core.NewModule(core.NewModuleOptions{\n        Imports:     []core.Modules{tslog.ForRoot(ContextHandler{Handler: base})},\n        Controllers: []core.Controllers{userController},\n        Middlewares: []core.Middleware{traceMiddleware, loggerMiddleware},\n    })\n}\n```\n\nEvery log line produced inside a request will now contain a `req_id` field automatically.\n\n---\n\n## API Reference\n\n### `ForRoot(h slog.Handler) core.Modules`\n\nRegisters a new `*slog.Logger` (backed by `h`) as the `TSLOG` provider in the module. Call this once in your root module's `Imports`.\n\n### `Inject(module core.Module) *slog.Logger`\n\nRetrieves the `*slog.Logger` registered by `ForRoot`. Returns `nil` if `ForRoot` was not imported.\n\n### `Middleware(options MiddlewareOptions) core.Middleware`\n\nReturns a Tinh Tinh middleware that:\n1. Skips execution for any path listed in `SkipPaths`.\n2. Calls `Fnc(ctx)` for all other requests.\n\n#### `MiddlewareOptions`\n\n| Field | Type | Description |\n|---|---|---|\n| `SkipPaths` | `[]string` | URL paths that bypass the middleware |\n| `Fnc` | `func(ctx core.Ctx)` | Logging logic executed per request |\n\n### `TSLOG`\n\nA `core.Provide` constant (`\"TS_LOG\"`) used as the DI token. Use `ctx.Ref(tslog.TSLOG)` inside middleware to retrieve the logger.\n\n---\n\n## Testing\n\n```bash\ngo test -cover ./...\n```\n\nThe CI pipeline runs tests against Go **1.24**, **1.25**, and **1.26**, with coverage reports uploaded to [Codecov](https://codecov.io/gh/tinh-tinh/tslog).\n\n---\n\n## License\n\nDistributed under the MIT License. See [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinh-tinh%2Ftslog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinh-tinh%2Ftslog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinh-tinh%2Ftslog/lists"}