@ciondigital/logger v1.0.13
Cion-Logger
A logger for cion project, where we can log required data at multiple levels.
Motivation
cion-logger
is designed to be a simple and cion logging library with
the support of winston and winston-mongodb. Where we can store logs in file or mongodb or just can show on console. Each cion-logger
logger configured at different levels (explained below in detail). For
example, one may want error logs to be stored in a persistent remote location
(like a database), but all logs output to the console or a local file.
Dependency
For using cion-logger
, you just need to add below variables as per requirement in your .env file.
NODE_ENV=dev - Set environment where you are deploying your code. LOG_LEVEL=error - Set log level, which ever level you want to print. MONGODB_COLLECTION_ADDED=false - Set true if you want to insert log in mongo otherwise set false.
Usage
For using cion-logger, you just need to add package name and version in package.json and install via npm or yarn. After that import whereever required and use as shown below.
Package.json:-
"dependencies": {
"@ciondigital/logger": "^1.0.12",
}
import wherever required:-
import logger from "@ciondigital/logger";
logger.debug(`Enter :: TestClass :: testMethode`);
logger.error(`Enter :: TestClass :: testMethode`);
logger.info(`Enter :: TestClass :: testMethode`);
Logging Levels
Logging levels conform to the severity ordering. severity of all levels is assumed to be numerically ascending from most important to least important. If you use lower level then you are going to print all the above levels. Ex.:- if you set level as "debug" then logger will print all the above logs like error, warning, info.
Each level
is given a specific integer priority. The higher the priority the
more important the message is considered to be, and the lower the
corresponding integer priority.
{
emerg: 0,
alert: 1,
crit: 2,
error: 3,
warning: 4,
notice: 5,
info: 6,
debug: 7
}
Similarly, npm
logging levels are prioritized from 0 to 6 (highest to
lowest):
{
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
}
If you do not explicitly define the levels that cion-logger
should use, the
npm
levels above will be used.
Recommendation
Use cion-logger
levels as below shown:-
- "logger.log" - We can use in general info like db connection info, ports, username etc.
- "logger.error" - We should use where we likely to get error. ex:- catch block.
- "logger.debug" - We can use for every where, whereever we want to debug our code. ex:- entering the method or class, general info etc.
- "logger.warn" - We should use where we want to show warning information. ex:- like partially success.
Using Logging Levels
Setting the level for your logging message can be accomplished in one of two ways. You can pass a string representing the logging level to the log() method.
//
// Any logger instance
//
logger.log("silly", "127.0.0.1 - there's no place like home");
logger.log("debug", "127.0.0.1 - there's no place like home");
logger.log("verbose", "127.0.0.1 - there's no place like home");
logger.log("info", "127.0.0.1 - there's no place like home");
logger.log("warn", "127.0.0.1 - there's no place like home");
logger.log("error", "127.0.0.1 - there's no place like home");
logger.info("127.0.0.1 - there's no place like home");
logger.warn("127.0.0.1 - there's no place like home");
logger.error("127.0.0.1 - there's no place like home");
//
// Default logger
//
winston.log("info", "127.0.0.1 - there's no place like home");
winston.info("127.0.0.1 - there's no place like home");