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

logson

v1.0.1

Published

Simple middleware library to get the application logs (Express) and do whatever you want with them

Downloads

5

Readme

logson

Build Status Gitter

Simple middleware library to get the application logs (Express) and do whatever you want with them

Install

With npm:

  $> npm install --save logson

API

  var logson = require('logson');

logson(options|callback, [callback])

You can use it with options or without, in this case the first param will be the callback.

  var app = require('express')();
  var logson = require('logson');

  app.use(logson(function(log) {
    // Then you can use the log here
  }));

With options:

  var app = require('express')();
  var logson = require('logson');

  var options = {
    parent: 'parent'
  }

  app.use(logson(options, function(log) {
    // Then you can use the log here
  }))

By default the format is the following:

  { }

req._logson

When you use logson it declares an attribute to the req object with the name _logson. You can set whatever you want to this attribute that will be merged to the final log.

Options

format

You can specify different types of log format (the array represents all the keys of the log):

  • combined
  ['remoteAddr', 'date', 'method', 'url', 'httpVersion', 'status', 'contentLength', 'referrer', 'userAgent']
  • common
  [ 'remoteAddr', 'date', 'method', 'httpVersion', 'status', 'contentLength']
  • dev
  [ 'method', 'status', 'responseTime', 'contentLength']
  • short
  [ 'remoteAddr', 'url', 'httpVersion', 'method', 'responseTime', 'contentLength' ]
  • tiny
  [ 'method', 'url', 'status', 'responseTime', 'contentLength' ]

parent

This options encapsulates the log object:

  var logson = require('logson');

  var options = {
    parent: 'parent'
  };

  logson(options, funciton(log) {
    // The log object is { parent: log }
  })

extras

With this option you can add extra fields to the JSON log, when extras are defined you can have access to the req and res objects:

  var logson = require('logson');

  var options = {
    extras: function(req, res) {
      return {
        cookies: req.cookies
      }
    }
  };

  logson(options, funciton(log) {
    // The log object has the cookies field on it
  })

extras use a deepmerge methology so the object returned with it will be merged with the log.

Merge order

There are so many configurations that merge between them so the order is important, to no override fields.

The ored of this merge is the following: req._logson + format + parent + extras

Examples

A simple Use Case for logson is to save the log object to the DB, for example elastic:

  // Your elastic configuration
  var elastic = require('./elastic');
  var app = require('express')();

  app.use(logson(function(log) {
    elastic.create({
      index: 'index',
      type: 'type',
      body: log
    });
  })

If you want to save the cookies and wrap the log into a request name use the following:

  // All your app configuration
  var cookieParser = require('cookie-parser');

  app.use(cookieParser('pass'));

  var conf = {
    parent: 'request',
    extras: function(req, res) {
      return {
        request: {
          headers: {
            cookeis: req.signedCookies
          }
        }
      }
    }
  };

  app.use(conf, logson(function(log) {
    elastic.create({
      index: 'index',
      type: 'type',
      body: log
    });
  })

License

MIT