Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhanced logger module #657

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ class Analytics {
let storageOptions = {};
if (options && options.logLevel) {
logger.setLogLevel(options.logLevel);
this.logLevel = options.logLevel;
}

if (options && options.setCookieDomain) {
Expand Down
9 changes: 7 additions & 2 deletions integrations/Amplitude/browser.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
108 changes: 108 additions & 0 deletions utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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;
utsabc marked this conversation as resolved.
Show resolved Hide resolved
this.scope = scope || "";
}

setLogLevel(levelStr) {
if (levelStr && typeof levelStr === "string") {
const lvlStr = levelStr.toUpperCase();
this.level = LogLevel[lvlStr] ? LogLevel[lvlStr].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) {
// prefix SDK identifier
let msg = `%c RS SDK`;

// format the log message using `scope`
if (this.scope) {
msg = `${msg} - ${this.scope}`;
}

// 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;
}
return logArgs;
}
}

export default Logger;