redis-log

0.5.5 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i redis-log

Weekly Downloads

0

Version

0.5.5

License

MIT

Unpacked Size

11.4 kB

Total Files

6

Last publish

Collaborators

  • patrickpissurno