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

redis-log

v0.5.5

Published

Yet another Node.js logging module. This is a lightweight, opinionated logging library, which is optimized for async/await (ES7) logging with as little boilerplate code as possible.

Downloads

4

Readme

redis-log

Yet another Node.js logging module. This is a lightweight, opinionated logging library, which is optimized for async/await (ES7) logging with as little boilerplate code as possible.

This library only supports error logging (at least for now).

Downloads

How it works

  1. Set the NAMEenvironment variable to the name of your project (e.g. Awesome Project). It will be used to group the logs.
  2. Require the module in the starting point of your application and call the init() static method (that file should be in the root of your project)
const Log = require('redis-log');
Log.init(__dirname); //this should only be called once
  1. Get an instance of the logger in each file where you're going to use it
const Log = require('redis-log');
const logger = Log.getLogger(__filename);
  1. Use the logger when calling some async stuff
async function exampleWithoutLogger(){
    const connection = await pg.connect();
    const { rows } = await connection.query('SELECT * FROM mytable WHERE id = $1 AND name = $2', [3, 'Test']);
    await connection.release();
}

async function exampleWithLogger(){
    const connection = await pg.connect();
    
    //in case of an exception, this method will still throw the original one, so you should probably still use try/catch
    //the difference is that it will also intercept and log the error to the Redis database
    const { rows } = await logger.try(connection.query, ['SELECT * FROM mytable WHERE id = $1 AND name = $2', [3, 'Test']]);
    
    await connection.release();
}

Output log example

{
    "timestamp": "18/04/2019 11:27",
    "message": "Failed to execute A(). Stack:",
    "stack": "TypeError: Cannot read property 'u' of null\n    at A (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:9:11)\n    at B (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:14:23)\n    at main (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:19:26)\n    at Object.<anonymous> (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:23:1)\n    at Module._compile (internal/modules/cjs/loader.js:689:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)",
    "arguments": []
}

/*
    TypeError: Cannot read property 'u' of null
        at A (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:9:11)
        at B (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:14:23)
        at main (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:19:26)
        at Object.<anonymous> (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:23:1)
        at Module._compile (internal/modules/cjs/loader.js:689:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
*/

Complete usage example

This is how some production code might look like

const pg = require('./databases').pg; //not required, just for example's sake

const Log = require('redis-log');
const logger = Log.init(__dirname).getLogger(__filename); //init should only be called once

async function doSomeAsyncWork(){
    const connection = await pg.connect();
    try {
        const { rows } = await connection.query('SELECT * FROM mytable WHERE id = $1 AND name = $2', [3, 'Test']);
        console.log(rows);
    }
    catch(ex){
        throw ex;
    }
    finally {
        await connection.release();
    }
}

logger.try(doSomeAsyncWork);

Available parameters (API Reference)

Log.init(dirname)

  • dirname: Required. Should be the content of the resolved path for the root folder of the application (usually __dirname of the starting point of the app)

Log.getLogger(filename, opts)

  • filename: Required. Should be the content of the __filename variable for the current module file
  • opts: Optional. This object is passed down to the Redis instance (ioredis), so you can configure things like host, port, and all the other options available at their docs

logger.try(asyncFunction, functionArguments, extraMessage, shouldThrow)

  • asyncFunction: Required. A function that returns a Promise
  • functionArguments: Optional. An array containing the arguments you want that function to receive
  • extraMessage: Optional. A string containing some extra information you want to be stored together with the log, such as some sort of label. For example: "myMethod() #1 crashed"
  • shouldThrow: Optional. Default: true. Whether the function should re-throw the exception in case the Promise throws

Sponsors

T.T. Burger

License

MIT License

Copyright (c) 2019 Patrick Pissurno

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.