Skip to content

Commit

Permalink
Updated code to handle Monitor Configs #39
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed Jun 17, 2022
1 parent aa6eb34 commit 596c415
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion libraries/js/examples/configs/jsonConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"env": "test",
"logfile": "./examples//configs/log.log",
"consoleOnly": false,
"logStatementPattern": "[ {component} ~ {status} ] : {message}",
"logStatementPattern": "[ {component} ~ {logLevel} ] : {message}",
"logFormat": "statement"
}
61 changes: 37 additions & 24 deletions libraries/js/examples/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
// import { PROTOCOLS } from "../logsmith/lib/specs.js";
import Logsmith from "../logsmith/logsmith.js";

const logg = new Logsmith({})
const logg = new Logsmith({
monitor: {
port: "8080",
server: "localhost",
publisher: {
publisher: "test"
},
context: {
context: "testcon"
}
}
})

const SampleLog = {
status: "running",
component : "logger",
message: "The Test is Fine"
}
// const SampleLog = {
// status: "running",
// component: "logger",
// message: "The Test is Fine"
// }

console.log(logg)

logg.WARN(SampleLog)
logg.INFO(SampleLog)
logg.SUCCESS(SampleLog)
logg.FAILURE(SampleLog)
logg.CRITICAL(SampleLog)
logg.LOG("TRAILING", SampleLog)
// logg.WARN(SampleLog)
// logg.INFO(SampleLog)
// logg.SUCCESS(SampleLog)
// logg.FAILURE(SampleLog)
// logg.CRITICAL(SampleLog)
// logg.LOG("TRAILING", SampleLog)

console.log()
// console.log()

logg.fetchConfigFromFile("./examples/configs/jsonConfig.json")
logg.WARN(SampleLog)
logg.INFO(SampleLog)
logg.SUCCESS(SampleLog)
logg.FAILURE(SampleLog)
logg.CRITICAL(SampleLog)
logg.LOG("TRAILING", SampleLog)
// logg.fetchConfigFromFile("./examples/configs/jsonConfig.json")
// logg.WARN(SampleLog)
// logg.INFO(SampleLog)
// logg.SUCCESS(SampleLog)
// logg.FAILURE(SampleLog)
// logg.CRITICAL(SampleLog)
// logg.LOG("TRAILING", SampleLog)


const logg1 = new Logsmith({
"logFormat" : "statement"
})
// const logg1 = new Logsmith({
// "logFormat": "statement"
// })


logg1.CRITICAL("test")
// logg1.CRITICAL("test")
7 changes: 6 additions & 1 deletion libraries/js/logsmith/lib/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export const LogFormats = {
STATEMENT: "statement"
}

export var logs = []
export var logs = []

const PROTOCOLS = {
"http" : "http://",
"https" : "https://"
}
5 changes: 4 additions & 1 deletion libraries/js/logsmith/logsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default class Logsmith {
this.consoleOnly = options.console_only || true
this.logFormat = options.logFormat || LogFormats.JSON
this.logStatementPattern = statement || DefaultLogStatementPattern
this.monitorLogging = options.monitorLogging || false
this.monitorConfigs = getMonitorConfigs(options)
this.compiledLogPattern = compile(this.logStatementPattern)
}

Expand All @@ -24,8 +26,9 @@ export default class Logsmith {
this.consoleOnly = configs.consoleOnly
this.logFormat = Object.values(LogFormats).includes(configs.logFormat) ? configs.logFormat : LogFormats.JSON
this.logStatementPattern = configs.logStatementPattern || DefaultLogStatementPattern
this.compiledLogPattern = compile(this.logStatementPattern)
this.monitorLogging = configs.monitorLogging || false
this.monitorConfigs = getMonitorConfigs(configs)
this.compiledLogPattern = compile(this.logStatementPattern)
} else {
return Error("File format error. Should be json or env.")
}
Expand Down
15 changes: 8 additions & 7 deletions libraries/js/logsmith/monitor/monitorConfigs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import compile from "string-template/compile.js"

const URITemplate = compile("{0}:{1}");
const URITemplate = compile("{0}://{1}:{2}");
const DefaultPublisherTemplate = {
Origin: compile("app.{0}.com"),
Description: compile("Logs Published by {0}")
Expand Down Expand Up @@ -32,18 +32,19 @@ function formatCotextConfig(publisher, monitorConfig) {

export function getMonitorConfigs(config) {
const monitorConfigs = {}
monitorConfigs.monitorPort = config.MONITOR.port || process.env.MONITOR_PORT
monitorConfigs.monitorURI = config.MONITOR.server || process.env.MONITOR_URI
monitorConfigs.monitorListener = URITemplate(monitorConfigs.monitorURI, monitorConfigs.monitorPort) || process.env.LISTENER
monitorConfigs.publisher = formatPublisherConfig(config.MONITOR.publisher)
monitorConfigs.context = formatCotextConfig(monitorConfigs.publisher.publisher, config.MONITOR.context)
monitorConfigs.monitorPort = config.monitor.port || process.env.MONITOR_PORT
monitorConfigs.monitorURI = config.monitor.server || process.env.MONITOR_URI
monitorConfigs.monitorProtocol = config.monitor.protocol || process.env.MONITOR_PROTOCOL || "http"
monitorConfigs.monitorListener = URITemplate(monitorConfigs.monitorProtocol, monitorConfigs.monitorURI, monitorConfigs.monitorPort) || process.env.LISTENER
monitorConfigs.publisher = formatPublisherConfig(config.monitor.publisher)
monitorConfigs.context = formatCotextConfig(monitorConfigs.publisher.publisher, config.monitor.context)
return monitorConfigs
}

// can be used for test
// console.log(
// getMonitorConfigs({
// MONITOR: {
// monitor: {
// port: "8080",
// server: "localhost",
// publisher: {
Expand Down

0 comments on commit 596c415

Please sign in to comment.