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

bitnacle-express

v1.3.3

Published

Dead simple middleware logger to use with Express apps

Downloads

9

Readme

bitnacle-express

Build Status Coverage Status David David GitHub npm

bitnacle-express is a dead simple middleware logger to use with Express apps.

It is compatible with request-ip and express-request-id, if you are using them make sure to use bitnacle-express after them.

Installation

npm i bitnacle-express

Quick start

const app = require('express')();
const bitnaclExpress = require('bitnacle-express');

app.use(bitnaclExpress.logger()); // use default "simple" format 

Usage

  • Log incoming HTTP requests using logger:

    You can specify 3 different formats: simple (default), json and extended

    app.use(bitnaclExpress.logger({
        format: 'json' // optional: default is "simple"
    }));  

    These are the outputs for the 3 different formats, simple, json and extended respectively:

    [2020-05-28T00:53:13:658+0200] [INFO] [GET] [/] [::1] [Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36] [cd820302-740b-4f30-8a68-a4348c68bdd9] [304] [9ms]
    {"time":"2019-08-25T17:04:47:603+0200","level":"INFO","method":"GET","endpoint":"/","remoteAddress":"::1","id":"6c09133d-ffa3-4ad3-af3b-8e5c78ee73ad","statusCode":304,"elapsedTime":"18ms"}
    { 
        time: '2019-08-25T17:13:52:079+0200',
        level: 'INFO',
        hostname: 'localhost',
        req: { 
            method: 'GET',
            endpoint: '/',
            headers: { 
                host: 'localhost:3100',
                connection: 'keep-alive',
                'cache-control': 'max-age=0',
                'upgrade-insecure-requests': '1',
                'user-agent':
                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
                'sec-fetch-mode': 'navigate',
                'sec-fetch-user': '?1',
                accept:
                'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
                'sec-fetch-site': 'none',
                'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'es-ES,es;q=0.9,en;q=0.8',
                cookie: 'io=ohrfY8fxk-ZqSXbYAAAC',
                'if-none-match': 'W/"424-gtDsLN/eQBxV76fISNK6wSdFMmY"' 
            },
            remoteAddress: '::1',
            params: 0,
            query: 0,
            id: '9ff20b68-f46d-4eb5-9ef3-9cb077de1677' 
        },
        statusCode: 200,
        elapsedTime: '2ms' 
    }

    IMPORTANT: As noted before, if you are using request-ip and/or express-request-id in your app, you must use bitnacle-express after them:

    const app = require('express')();
    
    ... 
    
    app.use(requestIp.mw())
    app.use(addRequestId())
    app.use(bitnaclExpress.logger())
  • Log errors using errorLogger

    The errorLogger only supports the simple and json formats. Your app must use it after the routes declaration.

    const router = require('./router');
    
    app.use(router);
    
    app.use(bitnaclExpress.errorLogger({
        format: 'json' // optional: default is "simple"
    }));

    IMPORTANT: errorLogger doesn't handle the errors/exceptions, it only logs them, so place it before your error handlers.

    I personally recommend to try/catch your routes and handle errors locally unless you have a general handler for some or all of your routes. You can use Bitnacle logger along with bitnacle-express to log your errors on the catch block of your routes.

Log levels

bitnacle-express.logger will use predefined log levels based on the response.statusCode.

  • statusCode >= 500 will use [ERROR] level
  • statusCode >= 400 will use [WARNING] level
  • statusCode >= 100 will use [INFO] level

Log to stream files

In order to log to files, you must create streams and pass them to bitnacle. You can add as many streams as you want:

const router = require('./router');

const writableStream = fs.createWriteStream('./access.log', { flags: 'a' });

app.use(router);

app.use(bitnaclExpress.errorLogger({
    streams: [
        writableStream
    ]
}));