logger-winston-pg

1.0.31 • Public • Published

Simple logger for Node.js applications

Do you want to log your Node.js app activity? Simple, let this package know what environment you are working at, and track your own logs on console, file log or PostgreSQL table!

Installation

        npm i logger-winston-pg

Software dependencies

To use this package correctly, you must have to require the following packages: winston, pg and dotenv

Usage

Using this package is actually simple. You just have to use two functions: configLogger for config local or production environment and log for log your activity.

You do not need to create a table for logs, package will automatically creates it with required columns if it is not created.

You have to create an object as environment variable with the following structure:

//This is the structure
    "env": {
        "DB_TECHNICAL_LOGS": {
            "DB_ENDPOINT": "localhost",             //Host
            "DB_PORT": 5432,                        //Port
            "DB_SCH": "public",                     //Schema
            "DB_SCHEMA": "postgres",                //Database
            "DB_USER": "postgres",                  //User
            "DB_PASSWORD": "secret",                //Password
            "DB_SSL_MODE": "disable",               //SSLMode (disable, require)
            "DB_CERTIFICATE": ""                    //Certificate only if server requires SSL connection
        },
        "TABLE_NAME": "LOGS_TABLE_NAME"             //Logs table name
    }

This is how must be as environment variable:

"env": {
    "DB_TECHNICAL_LOGS": "\"DB_ENDPOINT\"\"localhost\"\"DB_PORT\": 5432, \"DB_SCH\"\"public\"\"DB_SCHEMA\"\"postgres\"\"DB_USER\"\"postgres\"\"DB_PASSWORD\"\"secret\"\"DB_SSL_MODE\"\"disable\"\"DB_CERTIFICATE\"\"\" }",
    "TABLE_NAME": "TABLE_NAME"
}

Package will search a environment variable named DB_TECHNICAL_LOGS and will obtain DB params.

const {configLogger, log } = require('logger-winston-pg');
 
const isProduction = true;
const fileSize = 10000000;
const filesCount = 10;
 
//Sets logger behavior as production environment
//Not showing logs in console
configLogger(isProduction, fileSize, filesCount);
 
/*
    Firs argument determines level of log (I: Info, W: Warn, E: Error) and
    second argument is the message log.
*/
log('I', 'This is an INFO log recorded in LOGS table and file.');
 
//Set logger behavior as local or test environment
configLogger(!isProduction, fileSize, filesCount);
 
log('W', 'This is a WARN log recorded in LOGS table, file and shown in console.');

Note: check dev dependencies and install them!

Package Sidebar

Install

npm i logger-winston-pg

Weekly Downloads

1

Version

1.0.31

License

ISC

Unpacked Size

11.2 kB

Total Files

7

Last publish

Collaborators

  • darbeta17