Commit b58d98fd authored by Yash Soni's avatar Yash Soni

Updated Logger implementation with console logs

parent f1fd3edc
import {createLogger} from 'bunyan'
import {_mandatoryParam, _typeCheck, _existsIn} from './Utils'
import {stripColors} from 'colors';
const LogLevel = ["DEBUG","WARN","TRACE","INFO","ERROR"];
class Logger {
constructor(name = _mandatoryParam('name'), level){
_typeCheck('name', String);
_existsIn(level, LogLevel);
var logger = createLogger({
name: name,
level:(level ? level : 'info')
});
this.error = function(logParams){
logger.error(stripColors.red(logParams, 'ENTRY'));
};
this.info = function(logParams){
logger.info(stripColors.blue(logParams));
};
this.debug = function(logParams){
logger.debug(stripColors.yellow(logParams, 'ENTRY'));
};
this.entering = function(logParams){
logger.trace(stripColors.bgWhite(logParams, 'ENTRY'));
};
this.exiting = function(logParams){
logger.trace(stripColors.bgWhite(logParams, 'EXIT'));
};
};
}
export {Logger}
\ No newline at end of file
var Logger = require('./src/Logger').Logger;
module.exports = Logger;
\ No newline at end of file
......@@ -24,7 +24,6 @@
},
"homepage": "https://gitlab.com/yashsoni/easy-logging#readme",
"devDependencies": {
"bunyan": "^1.8.12",
"colors": "^1.3.3"
"chalk": "^2.4.2"
}
}
var _mandatoryParam = require('./Utils')._mandatoryParam;
var _typeCheck = require('./Utils')._typeCheck;
var _existsIn = require('./Utils')._existsIn;
const chalk = require('chalk').default;
const log = console.log;
const LogLevel = ["INFO","DEBUG","WARN","ERROR","TRACE"];
const LogLevelValues = {
"INFO": 10,
"DEBUG": 20,
"WARN": 30,
"ERROR": 40,
"TRACE": 50
}
const Logger = function (tag, level){
if(typeof tag == undefined || tag == null) {
_mandatoryParam('tag');
}
_typeCheck('tag', "string");
_existsIn(level, LogLevel);
// setting NAME & LEVEL
this.tag = tag;
this.level = (level ? String(level).toUpperCase() : "INFO");
// Util function to convert msg into logging string
function getLogJSONString(level, tag, msg, logParams, traceMeta) {
const result = {
tag: tag,
level: level,
timestamp: Date(),
extra: logParams
};
if(traceMeta) {
result.tracePoint = traceMeta;
result.methodName = msg;
} else {
result.msg = msg;
}
return JSON.stringify(result);
}
// Logging functions
this.error = function(msg, logParams){
if(LogLevelValues[this.level] >= LogLevelValues.ERROR)
log(chalk.red(getLogJSONString("ERROR", tag, msg, logParams)));
};
this.warn = function(msg, logParams){
if(LogLevelValues[this.level] >= LogLevelValues.WARN)
log(chalk.yellow(getLogJSONString("WARN", tag, msg, logParams)));
};
this.info = function(msg, logParams){
if(LogLevelValues[this.level] >= LogLevelValues.INFO)
log(chalk.cyan(getLogJSONString("INFO", tag, msg, logParams)));
};
this.debug = function(msg, logParams){
if(LogLevelValues[level] >= LogLevelValues.DEBUG)
log(chalk.blue(getLogJSONString("DEBUG", tag, msg, logParams)));
};
this.entering = function(methodName, logParams){
if(LogLevelValues[level] >= LogLevelValues.TRACE)
log(chalk.gray(getLogJSONString("TRACE", tag, methodName, logParams, "ENTRY")));
};
this.exiting = function(methodName, logParams){
if(LogLevelValues[level] >= LogLevelValues.TRACE)
log(chalk.gray(getLogJSONString("TRACE", tag, methodName, logParams, "EXIT")));
};
this.trace = function(msg, logParams){
if(LogLevelValues[level] >= LogLevelValues.TRACE)
log(chalk.gray(getLogJSONString("TRACE", msg, logParams)));
};
};
module.exports = {Logger}
\ No newline at end of file
......@@ -10,11 +10,12 @@ const _mandatoryParam = function (paramName) {
}
const _existsIn = function (value, container) {
value = String(value).toUpperCase();
if(typeof value == "string" && container.indexOf(value) == -1) {
return new Error(`${value} is not a valid value for the field: LogLevel. Please select one of the following: ${container}`);
}
}
export {
module.exports = {
_mandatoryParam, _typeCheck, _existsIn
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment