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

auditlogger

v0.3.0

Published

logger to control application logging

Downloads

31

Readme

Audit Logger

Build Status

Dependencies

Coverage Status

Provides more control to your logging.

Why

When running in production, you may wish you could have certain information avaialble for certain request. It is not efficient to log all information parameters for all requests. This library helps you to create application specific rules and provides request information data only when the application fails certain rules.

Install npm version

$ npm install auditlogger

How to use

Initialize auditlogger constructor

auditLogger(app, {ruleName1: function, ruleName2: function}, notifyCallback);
  • Parameter 1 - express application object.
  • Parameter 2 - Object, to specify logger rules. There can be any number of rules, each property consist of rule name and rule function. Module provides httpResponseCode, and httpResponseTime rules which can be configured. Custom rule function can be provided, if function return true, the rule is treated as failed and logger is turned on.
  • Parameter 3 - callback function which gets called if any of the rule fails. Function is invoked with express req object, res object and body passed to send.

AuditLogger automatically captures time taken for all middlewares and routes.

  • req.timer.value - gives time taken for all middleware and routes.
  • req.timer.start(name) and req.timer.stop(name) - could be used any time during the request to log private functions.
  • req.timer.value contains special $finalTimer key which contains total time taken for the request.

#####Note: name your middleware functions to get better timer results.

Usage

var express = require('express');
var app = express();
app.listen(3000);
var auditLogger = require('./../index.js');

auditLogger(app, {
    responseTime: httpResponseTime(500)
}, function loggingCallback(req, res) {
    //log your application data here
    console.log(req.headers);
    console.log(req.timers.value);
});

app.use(function testMiddleware(req, res, next) {
    setTimeout(function() {
        next();
    }, 200)
});
app.get('/', function defaultRoute(req, res) {
    privateFunction(req, function() {
        res.status(500).send('ok');
    });
});

function privateFunction(req, callback) {
	req.timers.start('privateFunction');
    setTimeout(function() {
    	req.timers.stop('privateFunction');
    	callback();
    }, 501);
}

function httpResponseTime(milliseconds) {
    //req.timers.$finalTimer contains total time for the request
    return function(req, res) {
        return req.timers.value.some(function(timer) {
            if (timer.$finalTimer > milliseconds) {
                return true;
            }
        });
    }
}

###Output req.headers

{ host: '127.0.0.1:3000',  'user-agent': 'curl/7.43.0',  accept: '*/*' }

req.timers.value

[ { testMiddleware: 201 },  { defaultRoute: 506 }, { 'defaultRoute -> privateFunction': 505 }, { '$finalTimer': 708 } ]

Examples

Examples are avaialbe here

Tests

$ npm install
$ npm test

License

The MIT License