{"id":25858498,"url":"https://github.com/cabinjs/parse-logs","last_synced_at":"2025-03-01T20:17:55.846Z","repository":{"id":33952038,"uuid":"163902309","full_name":"cabinjs/parse-logs","owner":"cabinjs","description":"Parse and validate logs to adhere to the message and meta standards from Lad and Cabin.  Made for Cabin and Lad.","archived":false,"fork":false,"pushed_at":"2023-02-15T00:04:11.000Z","size":528,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-27T08:23:00.852Z","etag":null,"topics":["arguments","express","format","koa","logger","logging","logs","message","metadata","middleware","node","parse","utility"],"latest_commit_sha":null,"homepage":"https://cabinjs.com","language":"JavaScript","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/cabinjs.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":"2019-01-03T00:05:41.000Z","updated_at":"2023-03-12T20:13:55.000Z","dependencies_parsed_at":"2023-01-15T03:33:56.824Z","dependency_job_id":null,"html_url":"https://github.com/cabinjs/parse-logs","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cabinjs%2Fparse-logs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cabinjs%2Fparse-logs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cabinjs%2Fparse-logs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cabinjs%2Fparse-logs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cabinjs","download_url":"https://codeload.github.com/cabinjs/parse-logs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241418357,"owners_count":19959736,"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":["arguments","express","format","koa","logger","logging","logs","message","metadata","middleware","node","parse","utility"],"created_at":"2025-03-01T20:17:55.221Z","updated_at":"2025-03-01T20:17:55.833Z","avatar_url":"https://github.com/cabinjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# parse-logs\n\n[![build status](https://github.com/cabinjs/parse-logs/actions/workflows/ci.yml/badge.svg)](https://github.com/cabinjs/parse-logs/actions/workflows/ci.yml)\n[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)\n[![license](https://img.shields.io/github/license/cabinjs/parse-logs.svg)](LICENSE)\n\n\u003e Parse and validate logs to adhere to the message and meta standards from [Lad][] and [Cabin][].\n\n\n## Table of Contents\n\n* [Install](#install)\n* [How does it work](#how-does-it-work)\n* [Usage](#usage)\n  * [Koa](#koa)\n  * [Express](#express)\n* [Contributors](#contributors)\n* [License](#license)\n\n\n## Install\n\n[npm][]:\n\n```sh\nnpm install parse-logs\n```\n\n\n## How does it work\n\nThis package exports a function that accepts two arguments `req` which is either `ctx.request` from [Koa][] or `req` from [Express][], and `userFields`, which is an Array of user fields to pick using [parse-request][]'s `userFields` option (by default it is simply `[ \"ip_address\" ]`).\n\nYou use this function to parse an inbound HTTP request body in order to return and validate a log object.\n\nIn order for this to work properly, the `body` must already be a parsed Object (it cannot be a String).\n\nFor example, below is an example request Object that you can pass as `req` to `parseLogs(req)`:\n\n```js\nconst parseLogs = require('.');\n\nconst req = {\n  method: 'GET',\n  query: {},\n  headers: {\n    'X-Request-Id': '123456',\n    'User-Agent': 'Test User Agent'\n  },\n  cookies: {},\n  body: {\n    err: {\n      message: 'Oops',\n      stack: '...'\n    },\n    message: 'Oops',\n    meta: {\n      level: 'error',\n      user: {\n        id: '123456',\n        email: 'test@example.com'\n      }\n    }\n  },\n  url: ''\n};\n\nconsole.log(parseLogs(req));\n```\n\nOutputs to console:\n\n```sh\n{\n  err: Error: Oops\n      at ... (::),\n  message: 'Oops',\n  meta: {\n    level: 'error',\n    user: { id: '123456', email: 'test@example.com' },\n    id: '636e9a831bdc98012abd4519',\n    timestamp: '2022-11-11T18:54:59.000Z',\n    request: { method: 'GET', headers: [Object], id: '123456' },\n    duration: 0.728459\n  }\n}\n```\n\nNote that there is a `user` object returned, which will be parsed from `req.user` automatically.\n\nThe `user` object will also have a `ip_address` property added, but only if one does not already exists and if an IP address was actually detected.\n\nAdditionally, `err`, `meta.err`, and `meta.original_err` properties from a request body payload will be parsed into Error objects with stack traces (normalized across Node and browser environments).\n\nFor an example implementation please refer to the [Forward Email][forward-email] codebase.\n\n\n## Usage\n\n### Koa\n\n```js\nconst parseLogs = require('parse-logs');\nconst bodyParser = require('koa-bodyparser');\n\napp.use(bodyParser());\napp.use((ctx, next) =\u003e {\n  const log = parseLogs(ctx.request);\n  console.log(log);\n  ctx.body = log;\n});\n```\n\n### Express\n\n```js\nconst parseLogs = require('parse-logs');\nconst bodyParser = require('body-parser');\n\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded());\napp.use((req, res, next) =\u003e {\n  const log = parseLogs(req);\n  console.log(log);\n  res.json(log);\n});\n```\n\n\n## Contributors\n\n| Name           | Website                    |\n| -------------- | -------------------------- |\n| **Nick Baugh** | \u003chttp://niftylettuce.com/\u003e |\n\n\n## License\n\n[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)\n\n\n##\n\n[npm]: https://www.npmjs.com/\n\n[cabin]: https://cabinjs.com\n\n[lad]: https://lad.js.org\n\n[koa]: https://koajs.com\n\n[express]: https://expressjs.com\n\n[parse-request]: https://github.com/cabinjs/parse-request\n\n[forward-email]: https://github.com/forwardemail/forwardemail.net\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcabinjs%2Fparse-logs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcabinjs%2Fparse-logs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcabinjs%2Fparse-logs/lists"}