/parse-logs

Parse and validate logs to adhere to the message and meta standards from Lad and Cabin. Made for Cabin and Lad.

Primary LanguageJavaScriptMIT LicenseMIT

parse-logs

build status code coverage code style styled with prettier made with lass license

Parse and validate logs to adhere to the message and meta standards from Lad and Cabin.

Table of Contents

Install

npm:

npm install parse-logs

yarn:

yarn add 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.

{
  request: {
    method: 'GET',
    query: {},
    headers: {},
    cookies: {},
    body: '',
    url: ''
  },
  user: {}
}

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.

Usage

Koa

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

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 http://niftylettuce.com/

License

MIT © Nick Baugh