npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-logs

v3.0.2

Published

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

Downloads

165

Readme

parse-logs

build status 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

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):

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:

{
  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 codebase.

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