Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
Angular console log only on development environment

In our Angular app (made with Angular CLI) we use several console statements. Is there a global way to detect environment and then display console.log in our components and service only under development?

What I mean by global way - I know we can use something like:

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

But by using this code everytime we have to console.log (along with necessary import to get environment variable) our code will become kind of verbose.

I want to know if there is a way to maybe:

  • access the environment in a quicker way
  • Maybe delete all console logs at prod build time

Or the better solution here is to create a logger service and do all the environment check within it?

I don't want my bundle size to be impacted by debug statements and service.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This overwrites all console logs with blank function.

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

}
over 4 years ago · Santiago Trujillo Report

0

Alternatively you can use a common service to achieve this

this.loggerService.log(this.reviewTasksList);

where as in your service you can use

log(text: string){
     if (!environment.production) {
       console.log(text)
     }
}
over 4 years ago · Santiago Trujillo Report

0

You can use isDevMode() or your environment.production to check if the code runs in development or production mode.

I think a logger service would be a good idea and then register a different logger service in providers depending on the mode.

See also https://github.com/angular/angular/pull/14308

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!