From 82478ab165e84a7abc3f0154d489de424718f26a Mon Sep 17 00:00:00 2001 From: Tanmoy Sen Gupta Date: Mon, 16 May 2022 19:42:00 +0000 Subject: [PATCH] Updated Code to add other LogLevel Methods #39 --- libraries/js/logsmith/lib/logUtility.js | 9 +++++-- libraries/js/logsmith/logsmith.js | 35 ++++++++++++++++++------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/libraries/js/logsmith/lib/logUtility.js b/libraries/js/logsmith/lib/logUtility.js index f7082e7..1295697 100644 --- a/libraries/js/logsmith/lib/logUtility.js +++ b/libraries/js/logsmith/lib/logUtility.js @@ -1,6 +1,10 @@ import chalk from 'chalk'; import format from 'string-template'; +function formatLogLevel(loglevel){ + return loglevel.padEnd(8) +} + export function prepareJSONLog(logLevel, log, env, callback) { const JSONLog = { @@ -12,9 +16,10 @@ export function prepareJSONLog(logLevel, log, env, callback) { callback(JSONLog) } -export function consoleLogJSON(chalkMode, JSONLog){ +export function consoleLogJSON(loglevel, chalkMode, JSONLog){ + loglevel = formatLogLevel(loglevel) console.log( - chalkMode(`[${JSONLog.logLevel}]`), + chalkMode(`[${loglevel}]`), JSON.stringify(JSONLog) ) } diff --git a/libraries/js/logsmith/logsmith.js b/libraries/js/logsmith/logsmith.js index eb1fc7d..7433010 100644 --- a/libraries/js/logsmith/logsmith.js +++ b/libraries/js/logsmith/logsmith.js @@ -6,13 +6,13 @@ import { readConfigFile } from './lib/fetchConfigs.js'; import { JSONLogDriver } from './lib/drivers.js'; import { consoleLogJSON, prepareJSONLog } from './lib/logUtility.js'; +const defaultLogPrintPattern = "[{level}] {body}" + const LogFormats = { JSON: "json", STATEMENT: "statement" } -const defaultLogPrintPattern = "[{level}] {body}" - const ChalkLog = { WARN: chalk.yellowBright, INFO: chalk.blue, @@ -55,28 +55,45 @@ export default class Logsmith { } } + INFO(log) { + if (this.logFormat == LogFormats.JSON) { + prepareJSONLog(LogLevels.INFO, log, this.env, function (JSONLog) { + consoleLogJSON(LogLevels.INFO, ChalkLog.INFO, JSONLog) + }) + } + } + WARN(log) { if (this.logFormat == LogFormats.JSON) { prepareJSONLog(LogLevels.WARN, log, this.env, function (JSONLog) { - consoleLogJSON(ChalkLog.WARN, JSONLog) + consoleLogJSON(LogLevels.WARN, ChalkLog.WARN, JSONLog) }) } } - INFO(log) { - - } CRITICAL(log) { - + if (this.logFormat == LogFormats.JSON) { + prepareJSONLog(LogLevels.CRITICAL, log, this.env, function (JSONLog) { + consoleLogJSON(LogLevels.CRITICAL, ChalkLog.CRITICAL, JSONLog) + }) + } } SUCCESS(log) { - + if (this.logFormat == LogFormats.JSON) { + prepareJSONLog(LogLevels.SUCCESS, log, this.env, function (JSONLog) { + consoleLogJSON(LogLevels.SUCCESS, ChalkLog.SUCCESS, JSONLog) + }) + } } FAILURE(log) { - + if (this.logFormat == LogFormats.JSON) { + prepareJSONLog(LogLevels.FAILURE, log, this.env, function (JSONLog) { + consoleLogJSON(LogLevels.FAILURE, ChalkLog.FAILURE, JSONLog) + }) + } } } \ No newline at end of file