From 6aeada563ebf5ff0ce7093dddd847e60984afbc6 Mon Sep 17 00:00:00 2001 From: Tanmoy Sen Gupta Date: Sat, 14 May 2022 08:07:18 +0000 Subject: [PATCH] Added String Templating #39 --- libraries/js/examples/index.js | 4 ++-- libraries/js/logsmith/logsmith.js | 13 +++++++++++-- libraries/js/logsmith/package-lock.json | 17 ++++++++++++++--- libraries/js/logsmith/package.json | 3 ++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/libraries/js/examples/index.js b/libraries/js/examples/index.js index 966ea45..9e3df12 100644 --- a/libraries/js/examples/index.js +++ b/libraries/js/examples/index.js @@ -1,4 +1,4 @@ -import Logsmith from "logsmith"; +import Logsmith from "../logsmith/logsmith.js"; const logg = new Logsmith({}) -console.log(logg.getParams()) \ No newline at end of file +console.log(logg.log({ level: "INFO", body: { col1: "val1", col2: "val2" } })) \ No newline at end of file diff --git a/libraries/js/logsmith/logsmith.js b/libraries/js/logsmith/logsmith.js index 3e7b23c..a05796d 100644 --- a/libraries/js/logsmith/logsmith.js +++ b/libraries/js/logsmith/logsmith.js @@ -1,7 +1,10 @@ import chalk from 'chalk'; +import format from 'string-template'; // const LISTENER +const defaultLogPrintPattern = "[{level}] {body}" + const logLevels = { WARN: chalk.yellow, INFO: chalk.blue, @@ -17,13 +20,19 @@ const logLevels = { // console.log(logLevels.FAILURE("FAILURE")) export default class Logsmith { - constructor(options) { + constructor(options, statement) { this.env = options.env || "default" this.logfile = options.logfile || null this.console_only = options.console_only || true + this.logPrintPattern = statement || defaultLogPrintPattern } getParams() { - return this.env + + } + + log(logbody) { + const logStatement = format(this.logPrintPattern, { level: logbody.level, body: JSON.stringify(logbody.body, null) }) + return logLevels.INFO(logStatement); } } \ No newline at end of file diff --git a/libraries/js/logsmith/package-lock.json b/libraries/js/logsmith/package-lock.json index 8e4ef10..4634fe6 100644 --- a/libraries/js/logsmith/package-lock.json +++ b/libraries/js/logsmith/package-lock.json @@ -1,15 +1,16 @@ { - "name": "logsmith.js", + "name": "logsmith", "version": "0.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "logsmith.js", + "name": "logsmith", "version": "0.0.1", "license": "ISC", "dependencies": { - "chalk": "^5.0.1" + "chalk": "^5.0.1", + "string-template": "^1.0.0" } }, "node_modules/chalk": { @@ -22,6 +23,11 @@ "funding": { "url": "https://github.com/chalk/chalk?sponsor=1" } + }, + "node_modules/string-template": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/string-template/-/string-template-1.0.0.tgz", + "integrity": "sha1-np8iM9wA8hhxjsN5oopWc+zKi5Y=" } }, "dependencies": { @@ -29,6 +35,11 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.0.1.tgz", "integrity": "sha512-Fo07WOYGqMfCWHOzSXOt2CxDbC6skS/jO9ynEcmpANMoPrD+W1r1K6Vx7iNm+AQmETU1Xr2t+n8nzkV9t6xh3w==" + }, + "string-template": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/string-template/-/string-template-1.0.0.tgz", + "integrity": "sha1-np8iM9wA8hhxjsN5oopWc+zKi5Y=" } } } diff --git a/libraries/js/logsmith/package.json b/libraries/js/logsmith/package.json index 692eb6b..044fd16 100644 --- a/libraries/js/logsmith/package.json +++ b/libraries/js/logsmith/package.json @@ -15,6 +15,7 @@ "author": "Tanmoy Sen Gupta", "license": "ISC", "dependencies": { - "chalk": "^5.0.1" + "chalk": "^5.0.1", + "string-template": "^1.0.0" } }