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:
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.
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 () { };
}
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)
}
}
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.