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

v1.2.3

Published

Dead simple logger module

Downloads

6

Readme

Bitnacle

Travis (.org) Coveralls github David David npm

Bitnacle is a dead simple logger module.

Installation

npm i bitnacle

Quick start

const Bitnacle = require('bitnacle');
const logger = new Bitnacle(); // Uses default "simple" format

logger.log({
    level: 'DEBUG',
    message: 'This is a debug message'
});

logger.debug('This is a debug message');

They both produce the same output:

[2019-08-25T15:49:47:928+0200] [DEBUG] [This is a debug message]

Formats

Bitnacle uses 2 formats:

  • simple: is the default format and logs plain text log messages
  • json: outputs json-stringified like log messages.

Bitnacle creates log messages with the following structure:

[:time] [:level] [:message]

To use the json format:

const jsonLogger = new Bitnacle({ format: 'json' });

jsonLogger.log({
    level: 'DEBUG',
    message: 'This is a debug message'
});

jsonLogger.debug('This is a debug message');
{"time":"2019-08-25T16:38:01:202+0200","level":"DEBUG","message":"This is a debug message"}

Loggin extra info

If you want to add more info to your loggs you can do it by passing an extraInfo object to Bitnacle.

IMPORTANT: if you create a nested object it won't be logged if using simple format, but json format will do it for you.

const extra = {
    someKey: 'someValue',
    anotherKey: 'anotherValue',
    yetAnotherKey: 'yetAnotherValue',
    someObjectProp: {
        someKeyInsideObjectProp: 'someValueInsideObjectProp'
    }
};

const extraInfo = { extra };
logger.debug('This is a debug message', extraInfo);

Outputs for simple and json formats, note the nested object is not being logged with simple format:

[2019-08-26T18:07:07:226+0200] [DEBUG] [someValue] [anotherValue] [yetAnotherValue] [This is a debug message]
{"time":"2019-08-26T18:07:42:949+0200","level":"DEBUG","someKey":"someValue","anotherKey":"anotherValue","yetAnotherKey":"yetAnotherValue","someObjectProp":{"someKeyInsideObjectProp":"someInsideObjectPropValue"},"message":"This is a debug message"}

Usage with express

If you are using Bitnacle with Express, you can pass the request object to Bitnacle for even more extra info. This is really usefull for debug purposes, since you can have Bitnacle and bitnacle-express working together identifying your requests easily.

Note that Bitnacle is compatible with request-ip and express-request-id and it will log the clientIp and id props if they are present on the request object.

This is the log message structure:

[:time] [:level] [:method] [:endpoint] [:remoteAddress] [:requestId] [:message]
app.get('/', function(req, res) {

    const extraInfo = { req };
    logger.debug('This is a debug message', extraInfo);

    ...

});

Outputs for simple and json formats:

[2019-08-25T16:07:52:686+0200] [DEBUG] [GET] [/] [::1] [cd657929-e0da-4f9b-ad92-d6a4551a7636] [This is a debug message]
{"time":"2019-08-25T16:36:08:810+0200","level":"DEBUG","method":"GET","endpoint":"/","remoteAddress":"::1","id":"e5c87f07-f635-4f31-b86a-42bab7a35494","message":"This is a debug message"}

More extra fun

It's not over yet!
Of course you can pass both extra and req objects in extraInfo and Bitnacle will log it, now you have tons of info! :D
Wikipedia who?

The log structure if using extra and req objects together would be:

[:time] [:level] [:method] [:endpoint] [:remoteAddress] [:requestId] [someValue] [anotherValue] [yetAnotherValue] [:message]

Outputs for simple and json formats:

[2019-08-26T18:18:50:817+0200] [DEBUG] [GET] [/] [::1] [9c9d856c-1684-41c0-893d-3d047e80a01c] [someValue] [anotherValue] [yetAnotherValue] [This is a debug message]
{"time":"2019-08-26T18:21:50:351+0200","level":"DEBUG","method":"GET","endpoint":"/","remoteAddress":"::1","id":"19f486b4-9510-4b66-84f1-90ebbadf4fcd","someKey":"someValue","anotherKey":"anotherValue","yetAnotherKey":"yetAnotherValue","someObjectProp":{"someKeyInsideObjectProp":"someInsideObjectPropValue"},"message":"This is a debug message"}

Just don't get too crazy logging whatever comes to your mind!

Log level API

Bitnacle exposes a predefined set of level functions you can use directly.

  • Default levels for Bitnacle are the following:
    • ERROR
    • WARNING
    • INFO
    • DEBUG
const logger = new Bitnacle({
    format: 'json' // optional: default is "simple"
});

logger.error('Your error message');
logger.warning('Your warning message');
logger.info('Your info message');
logger.debug('Your debug message');

If you want to use your own levels, you can specify the level using logger.log.

Of course you can pass the extraInfo object to logger.log for you daily dose of info!

app.get('/', function(req, res) {

    logger.log({
        level: 'YOUR-CUSTOM-LEVEL',
        message: 'Your log message',
        extraInfo: {
            req,
            extra
        }
    })

    ...
    
});

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 writableStream = fs.createWriteStream('./access.log', { flags: 'a' });

const logger = new Bitnacle({
    streams: [ writableStream ]
});