{"id":46191531,"url":"https://github.com/ridafkih/widelogger","last_synced_at":"2026-03-12T23:01:14.775Z","repository":{"id":339102988,"uuid":"1160528831","full_name":"ridafkih/widelogger","owner":"ridafkih","description":"Better logging through the accumulation of context throughout a request lifecycle, built on Pino and AsyncLocalStorage.","archived":false,"fork":false,"pushed_at":"2026-02-18T04:36:38.000Z","size":2696,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-03T01:36:53.176Z","etag":null,"topics":["logging","loggingsucks","typescript","widelog","widelogging"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/ridafkih.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-02-18T03:46:41.000Z","updated_at":"2026-02-19T10:33:51.000Z","dependencies_parsed_at":"2026-02-18T07:06:33.319Z","dependency_job_id":null,"html_url":"https://github.com/ridafkih/widelogger","commit_stats":null,"previous_names":["ridafkih/widelogger"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/ridafkih/widelogger","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ridafkih%2Fwidelogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ridafkih%2Fwidelogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ridafkih%2Fwidelogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ridafkih%2Fwidelogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ridafkih","download_url":"https://codeload.github.com/ridafkih/widelogger/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ridafkih%2Fwidelogger/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30448566,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T21:31:01.033Z","status":"ssl_error","status_checked_at":"2026-03-12T21:30:43.161Z","response_time":114,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":["logging","loggingsucks","typescript","widelog","widelogging"],"created_at":"2026-03-03T00:46:34.764Z","updated_at":"2026-03-12T23:01:14.769Z","avatar_url":"https://github.com/ridafkih.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003ewidelogger\u003c/h1\u003e\n  \u003cp\u003eStructured wide event logging for Node.js.\u003c/p\u003e\n  \u003cp\u003eAccumulate context throughout a request lifecycle, then flush it as a single rich event — inspired by \u003ca href=\"https://loggingsucks.com\"\u003eloggingsucks.com\u003c/a\u003e. Built on \u003ca href=\"https://github.com/pinojs/pino\"\u003ePino\u003c/a\u003e and \u003ccode\u003eAsyncLocalStorage\u003c/code\u003e for zero-leak context isolation across concurrent requests.\u003c/p\u003e\n  \u003cspan\u003e\n    \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e\n    \u003cspan\u003e\u0026nbsp;\u0026nbsp;·\u0026nbsp;\u0026nbsp;\u003c/span\u003e\n    \u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e\n    \u003cspan\u003e\u0026nbsp;\u0026nbsp;·\u0026nbsp;\u0026nbsp;\u003c/span\u003e\n    \u003ca href=\"#api\"\u003eAPI\u003c/a\u003e\n    \u003cspan\u003e\u0026nbsp;\u0026nbsp;·\u0026nbsp;\u0026nbsp;\u003c/span\u003e\n    \u003ca href=\"#contribute\"\u003eContribute\u003c/a\u003e\n  \u003c/span\u003e\n\u003c/div\u003e\n\u003chr\u003e\n\n## Installation\n\nTo install widelogger, simply use your favourite Node.js package manager.\n\n```bash\nbun add widelogger\n```\n\n```bash\npnpm add widelogger\n```\n\n```bash\nyarn add widelogger\n```\n\n```bash\nnpm install widelogger\n```\n\n## Usage\n\n### Creating a Logger\n\n```ts\nimport { widelogger } from \"widelogger\";\n\nconst { context, destroy } = widelogger({\n  service: \"checkout-api\",\n  defaultEventName: \"http_request\",\n  version: \"1.0.0\",\n  commitHash: process.env.COMMIT_SHA,\n});\n```\n\n`widelogger()` returns a `context` function to wrap request lifecycles and a `destroy` function for graceful shutdown. To log fields, import `widelog` directly from the package — it works anywhere inside a `context()` call thanks to `AsyncLocalStorage`.\n\n```ts\nimport { widelog } from \"widelogger\";\n\nwidelog.set(\"user.id\", userId);\n```\n\n### Express\n\nCreate a logger instance once, use middleware to wrap requests in a context, and accumulate fields from anywhere in your codebase by importing `widelog` directly.\n\n```ts\n// src/logger.ts\nimport { widelogger } from \"widelogger\";\n\nexport const { context, destroy } = widelogger({\n  service: \"checkout-api\",\n  defaultEventName: \"http_request\",\n});\n```\n\n```ts\n// src/middleware/logging.ts\nimport { widelog } from \"widelogger\";\nimport { context } from \"../logger\";\n\nexport const logging = (request, response, next) =\u003e {\n  context(\n    () =\u003e\n      new Promise((resolve) =\u003e {\n        widelog.set(\"method\", request.method);\n        widelog.set(\"path\", request.path);\n        widelog.time.start(\"duration_ms\");\n\n        response.on(\"finish\", () =\u003e {\n          widelog.set(\"status_code\", response.statusCode);\n          widelog.time.stop(\"duration_ms\");\n          widelog.flush();\n          resolve();\n        });\n\n        next();\n      }),\n  );\n};\n```\n\n```ts\n// src/routes/checkout.ts\nimport { widelog } from \"widelogger\";\n\nexport const checkout = async (request, response) =\u003e {\n  const { userId } = request.body;\n\n  widelog.set(\"user.id\", userId);\n  widelog.set(\"user.plan\", \"premium\");\n\n  widelog.time.start(\"db_ms\");\n  const order = await processOrder(userId);\n  widelog.time.stop(\"db_ms\");\n\n  widelog.set(\"order.total_cents\", order.totalCents);\n  widelog.count(\"order.items\", order.itemCount);\n\n  response.json({ orderId: order.id });\n};\n```\n\nThe handler doesn't need to know about context setup or flushing — it just imports `widelog` from the package and adds fields. `AsyncLocalStorage` ensures concurrent requests never leak into each other.\n\n### Bun\n\nThe same pattern works with Bun's built-in server.\n\n```ts\n// src/logger.ts\nimport { widelogger } from \"widelogger\";\n\nexport const { context, destroy } = widelogger({\n  service: \"checkout-api\",\n  defaultEventName: \"http_request\",\n});\n```\n\n```ts\n// src/routes/checkout.ts\nimport { widelog } from \"widelogger\";\n\nexport const checkout = async (request: Request) =\u003e {\n  const { userId } = await request.json();\n\n  widelog.set(\"user.id\", userId);\n  widelog.set(\"user.plan\", \"premium\");\n\n  widelog.time.start(\"db_ms\");\n  const order = await processOrder(userId);\n  widelog.time.stop(\"db_ms\");\n\n  widelog.set(\"order.total_cents\", order.totalCents);\n  widelog.count(\"order.items\", order.itemCount);\n\n  return Response.json({ orderId: order.id });\n};\n```\n\n```ts\n// src/server.ts\nimport { serve } from \"bun\";\nimport { widelog } from \"widelogger\";\nimport { context } from \"./logger\";\nimport { checkout } from \"./routes/checkout\";\n\nserve({\n  fetch: (request) =\u003e\n    context(async () =\u003e {\n      const url = new URL(request.url);\n      widelog.set(\"method\", request.method);\n      widelog.set(\"path\", url.pathname);\n      widelog.time.start(\"duration_ms\");\n\n      try {\n        const response = await checkout(request);\n        widelog.set(\"status_code\", response.status);\n        widelog.set(\"outcome\", \"success\");\n        return response;\n      } catch (error) {\n        widelog.set(\"outcome\", \"error\");\n        widelog.set(\"status_code\", 500);\n        widelog.errorFields(error);\n        return Response.json({ error: \"checkout failed\" }, { status: 500 });\n      } finally {\n        widelog.time.stop(\"duration_ms\");\n        widelog.flush();\n      }\n    }),\n});\n```\n\nInstead of scattered `console.log` calls, each request produces a single JSON log line containing service metadata, user info, order details, timing, and outcome.\n\nYou can see full working examples in the [examples/](./examples) directory for [Bun](./examples/bun), [Express](./examples/express), [Hono](./examples/hono), and [Fastify](./examples/fastify).\n\n### Dot Notation\n\nKeys use dot notation which expands to nested objects in the output. Keys are type-checked at compile time — empty strings, leading/trailing dots, and double dots are all rejected.\n\n```ts\nwidelog.set(\"user.id\", \"usr_123\");\nwidelog.set(\"user.plan\", \"premium\");\n// Output: { user: { id: \"usr_123\", plan: \"premium\" } }\n```\n\n### Log Routing\n\nEvents with `status_code \u003e= 500` or `outcome === \"error\"` are emitted at `error` level. Everything else is `info`. In development, logs are pretty-printed; in production, they're structured JSON.\n\n## API\n\n### `widelogger(options)`\n\nCreates a logger instance. Returns `{ context, destroy }`.\n\n| Option | Type | Description |\n|--------|------|-------------|\n| `service` | `string` | Service name (required) |\n| `defaultEventName` | `string` | Event name included in every log (required) |\n| `version` | `string` | Service version |\n| `commitHash` | `string` | Git commit hash (defaults to `\"unknown\"`) |\n| `instanceId` | `string` | Instance identifier (defaults to `process.pid`) |\n| `environment` | `string` | Environment name (defaults to `NODE_ENV`) |\n| `level` | `string` | Log level (defaults to `LOG_LEVEL` env or `\"info\"`) |\n\n### `context(fn)`\n\nRun a function inside an isolated async context. All `widelog` calls within this function (and any functions it calls) are scoped to this context. Supports both sync and async callbacks.\n\n### `widelog`\n\nImported directly from `\"widelogger\"`. All methods operate on the current async context established by `context()`.\n\n| Method | Description |\n|--------|-------------|\n| `set(key, value)` | Set a field value (last write wins) |\n| `count(key, amount?)` | Increment a counter (default +1) |\n| `append(key, value)` | Append a value to an array |\n| `max(key, value)` | Track the maximum value for a key |\n| `min(key, value)` | Track the minimum value for a key |\n| `time.start(key)` | Start a timer |\n| `time.stop(key)` | Stop a timer and record elapsed ms |\n| `errorFields(error, opts?)` | Extract error name, message, and stack |\n| `flush()` | Aggregate all operations and emit the event |\n\nAll methods are safe no-ops when called outside a `context()`.\n\n### `destroy()`\n\nFlushes the underlying Pino logger and closes transports. Call this on graceful shutdown.\n\n## Contribute\n\nFeel free to contribute to the repository. Pull requests and issues with feature requests are _super_ welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fridafkih%2Fwidelogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fridafkih%2Fwidelogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fridafkih%2Fwidelogger/lists"}