Angular clear all console logs in production

 

Angular – Clear All of Your Console Logs in Production Build with Just a Few Lines of Code



When we were developing an application, we probably add a lot of console.log statements everywhere in the code. Sometimes we display such kind of information that your users are not supposed to see, but you think that you're good because most users will not press F12 (most of the time).

Showing console logs is not a good practice,it is a creazy thing actually in the production.So we recommend not to use console logs in production. Then question comes,How to remove console.log from production.


I will show you a trick where you can remove all your console log statements, when in development mode, and clear all of it when in production mode.


Go to your main.ts file and add this:





if (environment.production) {

  enableProdMode();

  if(window){

    window.console.log=function(){};

  }

}


Now you have a clean all console log from your code at once, when you build your app for production.

Note:
In case you don’t know how to build for production, here’s the command

ng build --prod

Comments

Popular posts from this blog

Angular Best Practices and Coding Standards: A Guide for Developers

Route Web Server Request to App server through proxy

Top 7 ways to use Interceptors in Angular