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

simple-log-node

v1.0.2

Published

A simple logger for Node.js application provides JSON formatted logging and supports masking of PII (Personally Identifiable Information) data in logging.

Downloads

8

Readme

simple-log-node Logger

A simple logger for Node.js application provides JSON formatted logging and supports masking of PII (Personally Identifiable Information) data in logging.

Installation

npm install simple-log-node

Usage

    // require
	const Logger = require('simple-log-node);

    // instantiation
    // logging context
    const context = {
        name: <string>, // required
        application: <string>, //optional
        category: <string>, // optional
        type: <string>, // optional default to 'application'
        log_id: <any>, // optional 
        time: <timestamp> // optional 
    };

    // logging level
    const logLevel = 'info' or 'debug'; // defaults to info

    //mask options for masking PII fields in logs output
    const maskOptions = {
        maskFileds: <object_array> // default 'password' 
    }

    // create logger
    const logger = new Logger(
        context, // required
        logLevel, // optional
        maskOptions // optional
    );

    // logging

    logger.info('Something works fine!');

    //=> {"name":"Sample File","level":30,"category":"INFO","environment":"local","type":"application","log_id":"79afb8aa-c37f-41ff-b803-32ec98003403","data_object":{},"msg":"Something works fine!","time":"2020-06-20T17:43:14.951Z","v":0}

    logger.error('Something went wrong!');
    //=> {"name":"Sample File","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"79afb8aa-c37f-41ff-b803-32ec98003403","data_object":{},"msg":"Something went wrong!","time":"2020-06-20T17:43:14.955Z","v":0}

    logger.fatal('Something broken!');
    //=> {"name":"Sample File","level":60,"category":"FATAL","environment":"local","type":"application","log_id":"79afb8aa-c37f-41ff-b803-32ec98003403","data_object":{},"msg":"Something broken!","time":"2020-06-20T17:43:14.956Z","v":0}

    logger.debug('Something to debug!');
    //=> {"name":"Sample File","level":20,"category":"DEBUG","environment":"local","type":"application","log_id":"97019e69-f7e6-4522-813f-9477e5afd67a","data_object":{},"msg":"Something to debug!","time":"2020-06-20T17:45:49.735Z","v":0}

Examples

1. General Logging

// user.js

const Logger = require('simple-log-node');
const logger = new Logger( { name: 'User Class' } );

// let's create a sample user object
const user = {
	id: 100,
	name: 'John Doe',
	email: '[email protected]',
	mobile: '7777777777'
};

// logging with data
logger.info('User created successfully', user);
//=> {"name":"User Class","level":30,"category":"INFO","environment":"local","type":"application","log_id":"ae5163e2-c680-4d6b-b380-fc336ba8873d","data_object":{"id":100,"name":"John Doe","email":"[email protected]","mobile":"7777777777"},"msg":"User created successfully","time":"2020-06-20T18:13:58.875Z","v":0}

// logging with extra metadata
const someMetadata = {
 RequestId: "7823djbhbcj732"
};

logger.info('User created successfully', user, someMetadata);
//=> {"name":"User Class","level":30,"category":"INFO","environment":"local","type":"application","log_id":"1bad0c33-b926-4d14-80cf-57e7d1fd1827","RequestId":"7823djbhbcj732","data_object":{"id":100,"name":"John Doe","email":"[email protected]","mobile":"7777777777"},"msg":"User created successfully","time":"2020-06-20T18:20:10.820Z","v":0}

2. Logging with masking PII data

// user.js

const Logger = require('simple-log-node');

// configure masking options
const maskOptions = {
    maskFields: ['email', 'mobile']
};
const logger = new Logger( { name: 'User Class' },'info', maskOptions);

// let's create a sample user object
const user = {
    id: 100,
    name: 'John Doe',
    email: '[email protected]',
    mobile: '7777777777'
};

//logging message with masking `email` and `mobile`
logger.info(`User - ${user.id} created successfully`, user);

//=> {"name":"User Class","level":30,"category":"INFO","environment":"local","type":"application","log_id":"152428e7-4f2c-4597-b782-efdceee48f83","data_object":{"id":100,"name":"John Doe","email":"*******","mobile":"*******"},"msg":"User - 100 created successfully","time":"2020-06-20T18:34:57.290Z","v":0}

3. Logging Error Messages

// sample.js

const Logger = require('simple-log-node');

const logger = new Logger( { name: 'User Class' },'info');

// let's thrown an error
try {

    throw new TypeError("Something went wrong");

} catch (err) {

    // simple error message
    logger.error(`Error : ${err.message}`);

    //=> {"name":"User Class","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"fdf268e8-9971-4b44-ab2f-446ebbff424a","data_object":{},"msg":"Error : Something went wrong","time":"2020-06-20T18:54:56.330Z","v":0}

    // error message with stack trace
    logger.error(`Error : ${err.message}`, err.stack);

    //=> {"name":"User Class","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"fdf268e8-9971-4b44-ab2f-446ebbff424a","data_object":"TypeError: Something went wrong\n    at Object.<anonymous> (C:\\my-workspace\\simple-log-node\\test\\logger.test.js:6:11)\n    at Module._compile (internal/modules/cjs/loader.js:776:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)\n    at Module.load(internal/modules/cjs/loader.js:653:32)\n    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)\n    at Function.Module._load (internal/modules/cjs/loader.js:585:3)\n    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)\n    at startup (internal/bootstrap/node.js:283:19)\n    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","msg":"Error : Something went wrong","time":"2020-06-20T18:54:56.334Z","v":0}


    // error message with extra metadata
    logger.error(`Error : ${err.message}`, err.stack, {errorClass : err.name});

    //=> {"name":"User Class","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"fdf268e8-9971-4b44-ab2f-446ebbff424a","errorClass":"TypeError","data_object":"TypeError: Something went wrong\n    at Object.<anonymous> (C:\\my-workspace\\simple-log-node\\test\\logger.test.js:6:11)\n    at Module._compile (internal/modules/cjs/loader.js:776:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)\n    at Module.load (internal/modules/cjs/loader.js:653:32)\n    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)\n    at Function.Module._load (internal/modules/cjs/loader.js:585:3)\n    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)\n    at startup (internal/bootstrap/node.js:283:19)\n    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","msg":"Error : Something went wrong","time":"2020-06-20T18:54:56.334Z","v":0}

}

More examples will update soon!