Created
February 23, 2023 22:06
-
-
Save dhruvilp/381dd966d0b4306d77814f0490f13314 to your computer and use it in GitHub Desktop.
React remove console.log across the app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const GlobalDebug = (function () { | |
var savedConsole = console; | |
/** | |
* @param {boolean} debugOn | |
* @param {boolean} suppressAll | |
*/ | |
return function (debugOn, suppressAll) { | |
var suppress = suppressAll || false; | |
if (debugOn === false) { | |
// supress the default console functionality | |
console = {}; | |
console.log = function () {}; | |
// supress all type of consoles | |
if (suppress) { | |
console.info = function () {}; | |
console.warn = function () {}; | |
console.error = function () {}; | |
} else { | |
console.info = savedConsole.info; | |
console.warn = savedConsole.warn; | |
console.error = savedConsole.error; | |
} | |
} else { | |
console = savedConsole; | |
} | |
}; | |
})(); | |
// App.js | |
import React, { Suspense, useEffect } from "react"; | |
import { GlobalDebug } from "utils/remove-console"; | |
function App() { | |
/** | |
* @REMOVE_CONSOLES | |
* // remove the working of console logs | |
* // remove any accidental use of console logs | |
*/ | |
useEffect(() => { | |
(process.env.NODE_ENV === "production" || | |
process.env.REACT_APP_ENV === "STAGING") && | |
GlobalDebug(false); | |
}, []); | |
console.log("I am just another dummy console log, | |
suppose to be suppressed 🙂"); | |
return ( | |
<Suspense fallback={<h3>Loading...</h3>}> | |
<YourComponentsHere /> | |
</Suspense> | |
); | |
} | |
export default App; | |
// Source: https://dev.to/rajeshroyal/reactjs-disable-consolelog-in-production-and-staging-3l38 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment