From 4c68abe5ab103c14c8ace5b8424d36057383c17a Mon Sep 17 00:00:00 2001 From: Tanmoy Sen Gupta Date: Sun, 22 May 2022 11:03:00 +0000 Subject: [PATCH] Added Monitor Config Parser - #39 --- libraries/js/logsmith/logsmith.js | 3 + .../js/logsmith/monitor/monitorConfigs.js | 61 +++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/libraries/js/logsmith/logsmith.js b/libraries/js/logsmith/logsmith.js index f011627..8689a4e 100644 --- a/libraries/js/logsmith/logsmith.js +++ b/libraries/js/logsmith/logsmith.js @@ -2,6 +2,7 @@ import * as path from 'path'; import compile from 'string-template/compile.js'; import { loggerRunner } from './lib/drivers.js'; import { readConfigFile } from './lib/fetchConfigs.js'; +import { getMonitorConfigs } from './monitor/monitorConfigs.js'; import { LogFormats, LogLevels, DefaultLogStatementPattern } from './lib/specs.js'; @@ -24,11 +25,13 @@ export default class Logsmith { this.logFormat = Object.values(LogFormats).includes(configs.logFormat) ? configs.logFormat : LogFormats.JSON this.logStatementPattern = configs.logStatementPattern || DefaultLogStatementPattern this.compiledLogPattern = compile(this.logStatementPattern) + this.monitorConfigs = getMonitorConfigs(configs) } else { return Error("File format error. Should be json or env.") } } + INFO(log) { loggerRunner( LogLevels.INFO, diff --git a/libraries/js/logsmith/monitor/monitorConfigs.js b/libraries/js/logsmith/monitor/monitorConfigs.js index 868b27e..671fd40 100644 --- a/libraries/js/logsmith/monitor/monitorConfigs.js +++ b/libraries/js/logsmith/monitor/monitorConfigs.js @@ -1,4 +1,57 @@ -export function getMonitorConfigs(params) { - var monitorAddresses = [] - -} \ No newline at end of file +import compile from "string-template/compile.js" + +const URITemplate = compile("{0}:{1}"); +const DefaultPublisherTemplate = { + Origin: compile("app.{0}.com"), + Description: compile("Logs Published by {0}") +} +const DefaultContextTemplate = { + Origin: compile("app.{0}.com/{1}"), + Description: compile("Log Published by {0} to context {1}"), + Kind: { + logs: [] + } +} + +function formatPublisherConfig(monitorConfig) { + const publisherConfig = {} + publisherConfig.publisher = monitorConfig.publisher || process.env.PUBLISHER + publisherConfig.origin = monitorConfig.origin || DefaultPublisherTemplate.Origin(publisherConfig.publisher) + publisherConfig.description = monitorConfig.description || DefaultPublisherTemplate.Description(publisherConfig.publisher) + return publisherConfig +} + +function formatCotextConfig(publisher, monitorConfig) { + const contextConfig = {} + contextConfig.context = monitorConfig.context || process.env.CONTEXT + contextConfig.origin = monitorConfig.origin || DefaultContextTemplate.Origin(publisher, contextConfig.context) + contextConfig.description = monitorConfig.description || DefaultPublisherTemplate.Description(publisher, contextConfig.context) + contextConfig.kind = monitorConfig.kind || DefaultContextTemplate.Kind + return contextConfig +} + +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) + return monitorConfigs +} + +// can be used for test +// console.log( +// getMonitorConfigs({ +// MONITOR: { +// port: "8080", +// server: "localhost", +// publisher: { +// publisher: "test" +// }, +// context: { +// context: "testcon" +// } +// } +// }) +// ) \ No newline at end of file