Skip to content

Instantly share code, notes, and snippets.

@cyberinferno
Created September 22, 2022 04:59
Show Gist options
  • Save cyberinferno/1acdac59bf6fadf9588de12f2d3306dd to your computer and use it in GitHub Desktop.
Save cyberinferno/1acdac59bf6fadf9588de12f2d3306dd to your computer and use it in GitHub Desktop.
import axios from "axios";
import { isDevMode } from "utils/core";
/**
* Returns an identical console object that can send logs to a log server
* if URL is specified
*
* @param remoteUrl? URL to send logs to
* @returns {{debug: () => void, error: () => void, info: () => void, log: () => void, warn: () => void}} Remote console object
*/
function getRemoteLogger(remoteUrl = null) {
const remoteLogger = {};
const sendData = async (data) => {
if (!remoteUrl) {
return new Promise((resolve) => {
resolve();
});
}
return axios.post(remoteUrl, data);
};
remoteLogger.debug = (...args) => {
sendData({ type: "debug", data: args });
if (isDevMode()) {
console.debug(...args);
}
};
remoteLogger.error = (...args) => {
sendData({ type: "error", data: args });
if (isDevMode()) {
console.error(...args);
}
};
remoteLogger.info = (...args) => {
sendData({ type: "info", data: args });
if (isDevMode()) {
console.info(...args);
}
};
remoteLogger.log = (...args) => {
sendData({ type: "log", data: args });
if (isDevMode()) {
console.log(...args);
}
};
remoteLogger.warn = (...args) => {
sendData({ type: "warn", data: args });
if (isDevMode()) {
console.warn(...args);
}
};
return remoteLogger;
}
export default getRemoteLogger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment