From f4d6fac64b4a6a7bd55fa40bbac7443a25338cb9 Mon Sep 17 00:00:00 2001 From: saikumarrs Date: Tue, 20 Sep 2022 21:35:48 +0530 Subject: [PATCH 1/3] feat: enhanced logger module --- analytics.js | 1 + integrations/Amplitude/browser.js | 9 ++- utils/logger.js | 93 +++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 utils/logger.js diff --git a/analytics.js b/analytics.js index cc94ee767..a01b6e8ab 100644 --- a/analytics.js +++ b/analytics.js @@ -1086,6 +1086,7 @@ class Analytics { let storageOptions = {}; if (options && options.logLevel) { logger.setLogLevel(options.logLevel); + this.logLevel = options.logLevel; } if (options && options.setCookieDomain) { diff --git a/integrations/Amplitude/browser.js b/integrations/Amplitude/browser.js index c7e69b5da..0a2a6188b 100644 --- a/integrations/Amplitude/browser.js +++ b/integrations/Amplitude/browser.js @@ -1,14 +1,17 @@ /* eslint-disable no-underscore-dangle */ /* eslint-disable class-methods-use-this */ -import logger from "../../utils/logUtil"; +import Logger from "../../utils/logger"; import { type } from "../../utils/utils"; import { LOAD_ORIGIN } from "../ScriptLoader"; import { NAME } from "./constants"; +const logger = new Logger(NAME); + class Amplitude { constructor(config, analytics) { this.name = NAME; this.analytics = analytics; + if (analytics.logLevel) logger.setLogLevel(analytics.logLevel); this.apiKey = config.apiKey; this.trackAllPages = config.trackAllPages || false; this.trackNamedPages = config.trackNamedPages || false; @@ -431,7 +434,9 @@ class Amplitude { // If price not present set price as revenue's value and force quantity to be 1. // Ultimately set quantity to 1 if not already present from above logic. if (!revenue && !price) { - console.debug("revenue or price is not present."); + logger.warn( + 'Neither "revenue" nor "price" is available. Hence, not logging revenue' + ); return; } diff --git a/utils/logger.js b/utils/logger.js new file mode 100644 index 000000000..7fdd7dbe7 --- /dev/null +++ b/utils/logger.js @@ -0,0 +1,93 @@ +const LogLevel = { + LOG: { + value: 0, + method: console.log, + }, + INFO: { + value: 1, + method: console.info, + }, + DEBUG: { + value: 2, + method: console.debug, + }, + WARN: { + value: 3, + method: console.warn, + }, + ERROR: { + value: 4, + method: console.error, + }, +}; + +class Logger { + constructor(scope, level) { + this.level = +level || LogLevel.ERROR.value; + this.scope = scope || ""; + } + + setLogLevel(levelStr) { + this.level = levelStr + ? LogLevel[levelStr.toString().toUpperCase()].value + : this.level; + } + + setScope(scopeVal) { + this.scope = scopeVal || this.scope; + } + + log(...args) { + this.logBase(args, LogLevel.LOG.value); + } + + info(...args) { + this.logBase(args, LogLevel.INFO.value); + } + + debug(...args) { + this.logBase(args, LogLevel.DEBUG.value); + } + + warn(...args) { + this.logBase(args, LogLevel.WARN.value); + } + + error(...args) { + this.logBase(args, LogLevel.ERROR.value); + } + + logBase(args, logLevel) { + if (this.level <= logLevel) { + const logVal = Object.values(LogLevel).find( + (val) => val.value === logLevel + ); + logVal.method(...this.getLogData(args)); + } + } + + /** + * Formats the console message using `scope` + * @param {*} logArgs + * @returns updated log arguments + */ + getLogData(logArgs) { + if (Array.isArray(logArgs) && logArgs.length > 0) { + let msg = `%c RS SDK `; + // format the log message using `scope` + if (this.scope) { + msg = `${msg}- ${this.scope}`; + } + msg = `${msg} %c ${logArgs[0].trim()}`; + const retArgs = []; + retArgs.push(msg); + retArgs.push("font-weight: bold; background: black; color: white;"); + retArgs.push("font-weight: normal;"); + retArgs.push(...logArgs.slice(1)); + return retArgs; + } + return logArgs; + } +} + +export default Logger; From 040719194e5087d6a1cb65731ee94df17be717ad Mon Sep 17 00:00:00 2001 From: saikumarrs Date: Tue, 20 Sep 2022 22:57:42 +0530 Subject: [PATCH 2/3] refactor: add documentation --- utils/logger.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/utils/logger.js b/utils/logger.js index 7fdd7dbe7..07c6e1783 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -73,16 +73,30 @@ class Logger { */ getLogData(logArgs) { if (Array.isArray(logArgs) && logArgs.length > 0) { - let msg = `%c RS SDK `; + // prefix SDK identifier + let msg = `%c RS SDK`; + // format the log message using `scope` if (this.scope) { - msg = `${msg}- ${this.scope}`; + msg = `${msg} - ${this.scope}`; } - msg = `${msg} %c ${logArgs[0].trim()}`; + + // trim whitespaces + const orgMsg = logArgs[0].trim(); + + // prepare the final message + msg = `${msg} %c ${orgMsg}`; + const retArgs = []; retArgs.push(msg); + + // add style for the prefix retArgs.push("font-weight: bold; background: black; color: white;"); + + // reset the style for the actual message retArgs.push("font-weight: normal;"); + + // append rest of the original arguments retArgs.push(...logArgs.slice(1)); return retArgs; } From 65be339f4c7158bda550511e3a5a15c66adee3da Mon Sep 17 00:00:00 2001 From: saikumarrs Date: Thu, 22 Sep 2022 15:26:54 +0530 Subject: [PATCH 3/3] fix: invalid level string scenario --- utils/logger.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/utils/logger.js b/utils/logger.js index 07c6e1783..3aac7151f 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -28,9 +28,10 @@ class Logger { } setLogLevel(levelStr) { - this.level = levelStr - ? LogLevel[levelStr.toString().toUpperCase()].value - : this.level; + if (levelStr && typeof levelStr === "string") { + const lvlStr = levelStr.toUpperCase(); + this.level = LogLevel[lvlStr] ? LogLevel[lvlStr].value : this.level; + } } setScope(scopeVal) {