Skip to content

Commit

Permalink
Updated Code to add other LogLevel Methods #39
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed Jun 17, 2022
1 parent d0fee8b commit 82478ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
9 changes: 7 additions & 2 deletions libraries/js/logsmith/lib/logUtility.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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)
)
}
Expand Down
35 changes: 26 additions & 9 deletions libraries/js/logsmith/logsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
})
}
}

}

0 comments on commit 82478ab

Please sign in to comment.