https://github.com/cabinjs/parse-logs
Parse and validate logs to adhere to the message and meta standards from Lad and Cabin. Made for Cabin and Lad.
https://github.com/cabinjs/parse-logs
arguments express format koa logger logging logs message metadata middleware node parse utility
Last synced: 3 months ago
JSON representation
Parse and validate logs to adhere to the message and meta standards from Lad and Cabin. Made for Cabin and Lad.
- Host: GitHub
- URL: https://github.com/cabinjs/parse-logs
- Owner: cabinjs
- License: mit
- Created: 2019-01-03T00:05:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-02-15T00:04:11.000Z (over 2 years ago)
- Last Synced: 2024-04-27T08:23:00.852Z (about 1 year ago)
- Topics: arguments, express, format, koa, logger, logging, logs, message, metadata, middleware, node, parse, utility
- Language: JavaScript
- Homepage: https://cabinjs.com
- Size: 516 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parse-logs
[](https://github.com/cabinjs/parse-logs/actions/workflows/ci.yml)
[](https://github.com/sindresorhus/xo)
[](https://github.com/prettier/prettier)
[](https://lass.js.org)
[](LICENSE)> Parse and validate logs to adhere to the message and meta standards from [Lad][] and [Cabin][].
## Table of Contents
* [Install](#install)
* [How does it work](#how-does-it-work)
* [Usage](#usage)
* [Koa](#koa)
* [Express](#express)
* [Contributors](#contributors)
* [License](#license)## Install
[npm][]:
```sh
npm install parse-logs
```## How does it work
This 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" ]`).
You use this function to parse an inbound HTTP request body in order to return and validate a log object.
In order for this to work properly, the `body` must already be a parsed Object (it cannot be a String).
For example, below is an example request Object that you can pass as `req` to `parseLogs(req)`:
```js
const parseLogs = require('.');const req = {
method: 'GET',
query: {},
headers: {
'X-Request-Id': '123456',
'User-Agent': 'Test User Agent'
},
cookies: {},
body: {
err: {
message: 'Oops',
stack: '...'
},
message: 'Oops',
meta: {
level: 'error',
user: {
id: '123456',
email: '[email protected]'
}
}
},
url: ''
};console.log(parseLogs(req));
```Outputs to console:
```sh
{
err: Error: Oops
at ... (::),
message: 'Oops',
meta: {
level: 'error',
user: { id: '123456', email: '[email protected]' },
id: '636e9a831bdc98012abd4519',
timestamp: '2022-11-11T18:54:59.000Z',
request: { method: 'GET', headers: [Object], id: '123456' },
duration: 0.728459
}
}
```Note that there is a `user` object returned, which will be parsed from `req.user` automatically.
The `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.
Additionally, `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).
For an example implementation please refer to the [Forward Email][forward-email] codebase.
## Usage
### Koa
```js
const parseLogs = require('parse-logs');
const bodyParser = require('koa-bodyparser');app.use(bodyParser());
app.use((ctx, next) => {
const log = parseLogs(ctx.request);
console.log(log);
ctx.body = log;
});
```### Express
```js
const parseLogs = require('parse-logs');
const bodyParser = require('body-parser');app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use((req, res, next) => {
const log = parseLogs(req);
console.log(log);
res.json(log);
});
```## Contributors
| Name | Website |
| -------------- | -------------------------- |
| **Nick Baugh** | |## License
[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)
##
[npm]: https://www.npmjs.com/
[cabin]: https://cabinjs.com
[lad]: https://lad.js.org
[koa]: https://koajs.com
[express]: https://expressjs.com
[parse-request]: https://github.com/cabinjs/parse-request
[forward-email]: https://github.com/forwardemail/forwardemail.net