ReactJS – Disable console.log() in production and staging

Husky hooks are a really good way to prevent someone from pushing accidental console logs to a production ready build or staging environment, but still if you decided to remove the usages of console.logs() from your build add this global function to yo…


This content originally appeared on DEV Community and was authored by Rajesh Royal

Husky hooks are a really good way to prevent someone from pushing accidental console logs to a production ready build or staging environment, but still if you decided to remove the usages of console.logs() from your build add this global function to your JS codebase.

remove-consoles.js

export const GlobalDebug = (function () {
  var savedConsole = console;
  /**
   * @param {debugOn} boolean
   * @param {suppressAll} boolean
   */
  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;
    }
  };
})();

Use this function at the root of your project or in any file you want to.

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;

⚠ Will update this blog with - how to set multiple enviornments in ReactJS [development, staging, production][through .env files]

Hope you enjoyed reading, make that heart read color in top left corner if you liked this post. 🍻


This content originally appeared on DEV Community and was authored by Rajesh Royal


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Royal | Sciencx (2022-01-30T08:01:10+00:00) ReactJS – Disable console.log() in production and staging. Retrieved from https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/

MLA
" » ReactJS – Disable console.log() in production and staging." Rajesh Royal | Sciencx - Sunday January 30, 2022, https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/
HARVARD
Rajesh Royal | Sciencx Sunday January 30, 2022 » ReactJS – Disable console.log() in production and staging., viewed ,<https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/>
VANCOUVER
Rajesh Royal | Sciencx - » ReactJS – Disable console.log() in production and staging. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/
CHICAGO
" » ReactJS – Disable console.log() in production and staging." Rajesh Royal | Sciencx - Accessed . https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/
IEEE
" » ReactJS – Disable console.log() in production and staging." Rajesh Royal | Sciencx [Online]. Available: https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/. [Accessed: ]
rf:citation
» ReactJS – Disable console.log() in production and staging | Rajesh Royal | Sciencx | https://www.scien.cx/2022/01/30/reactjs-disable-console-log-in-production-and-staging/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.