Solvedangular cli Suggestion: Remove console.log in production build

I have tons of console.log-calls in my code which is useful in the development, but something I wouldn't want to push to the console for end users.

I think it would be great to be able to config an environment something like this:

export const environment = {
  production: true,
  remove_console_log: true,
  remove_console_warning: true

This would build for production and simply remove console.log- and console.warning-calls while keeping console.error. Perhaps even "remove_debugger" (debugger;) and "remove_alert" (alert()) could be added.

I'm not sure if it's allowed to post suggestions/requests here, if not you're may just delete the post.

14 Answers

✔️Accepted Answer

@janjachacz I think you should mention that once ejected there is no turning back.

Other Answers:

Just create a wrapper function on top of console functions, and use it. For example

log(...args) {
    if (!production) {
        console.log.apply(console, args);

Am I single person who thinks that wrapper function or setting to an empty function are bad solutions?
If I don't want to show something in console why do I need it to be presented in a bundle?
I know it's a tiny amount of kilobytes and runtime milliseconds but it's just wrong to me.
Stripping console log was possible for grunt, gulp, webpack, I don't get why not letting users to choose if they want to strip it from bundle code or not.

Our environment file functionality just allows you to conditionally replace a file import with another file. We don't really process or interpret it.

My suggestion for your scenario is to use ng lint instead, and configure tslint.json to not allow console.log statements:

I'm sorry, I misunderstood the usecase and offered a bad suggestion. A better suggestion would be to use a logger service that wraps console.log and checks the environment, similar to what is suggested in the comment above.

More Issues: