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 = ; const isProduction = true;const fileSize = 10000000;const filesCount = 10; //Sets logger behavior as production environment//Not showing logs in console; /* Firs argument determines level of log (I: Info, W: Warn, E: Error) and second argument is the message log.*/; //Set logger behavior as local or test environment; ;
Note: check dev dependencies and install them!