Angular console log in development environment only

In our Angular application (with the Angular CLI) we use several operators console. Is there a global way to detect the environment and then display it console.login our components and services only during development?

What do I mean by the global path - I know that we can use something like:

if (!environment.production) {
  console.log(this.reviewTasksList);
}

But using this code every time we need console.log(along with the necessary import to get the variable environment), our code will become a kind of detailed.

I want to know if there is a way:

  • faster access to the environment.
  • Perhaps delete all console logs during prod build.

Or is the best solution here to create a registration service and do all the checking of the environment inside it?

, .

+12
5

this.loggerService.log(this.reviewTasksList);

, ,

log(text: string){
     if (!environment.production) {
       console.log(text)
     }
}
+5

.

if (environment.production) {
  enableProdMode();
  window.console.log = function () { };   // disable any console.log debugging statements in production mode
  // window.console.error = function () { };

}
+4

, console.log, dev?

 if (! isDevMode()){
   console.log = (...args)=>{}
 }
+3

, , . . @sedeh/smart-service SmartConsoleService . . . Smart-console , - , .

0

Source: https://habr.com/ru/post/1016821/


All Articles