Skip to content

Commit

Permalink
Added String Templating #39
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG authored May 14, 2022
1 parent e11defa commit 6aeada5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
4 changes: 2 additions & 2 deletions libraries/js/examples/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Logsmith from "logsmith";
import Logsmith from "../logsmith/logsmith.js";

const logg = new Logsmith({})
console.log(logg.getParams())
console.log(logg.log({ level: "INFO", body: { col1: "val1", col2: "val2" } }))
13 changes: 11 additions & 2 deletions libraries/js/logsmith/logsmith.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}
}
17 changes: 14 additions & 3 deletions libraries/js/logsmith/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion libraries/js/logsmith/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"author": "Tanmoy Sen Gupta",
"license": "ISC",
"dependencies": {
"chalk": "^5.0.1"
"chalk": "^5.0.1",
"string-template": "^1.0.0"
}
}

0 comments on commit 6aeada5

Please sign in to comment.