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

Published Oct 20, 2017
Angular – Clear All of Your Console Logs in Production Build with Just a Few Lines of Code

When you were developing an application, you probably inserted a lot of console.log statements everywhere in your code. Sometimes you display 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).

Not only it this a bad practice, you are also being lazy.

I will show you a trick where you can retain all of 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 console log 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

Thanks for reading and you don't need to be ashamed of your log statements 😃

Discover and read more posts from Brian
get started