How to remove JavaScript console.log - pros&cons

How to remove JavaScript console.log - pros&cons

A few days ago I wrote a post: Disable JavaScript console on production. Shortly after that, I had a discussion with a friend about other ways to remove console.* statements. I felt that the conclusions of that conversion are useful, so I decided to share them with you.

Method 1: remove it manually

The header is self explanatory. If we decide to go down this route, we need to search and remove all variations of console.* statements. For example: console.trace, console.time, console.warn, ...

In this case, a regular expression like console[.]\w[(].+[)] will be useful

Pros

console.* statements are removed from our codebase

Cons

it's manual

it needs to be run after every single commit

there's no way to turn logging on, on demand (i.e to troubleshoot a crash)

did I mention that it is manual?

Of course we can run some static code analyst to find out all the console.* statements. Event JSLint will be enough

Method 2: build step

We can automate the previous steps using our build tools. A couple of examples are included below:

Method 3: Using a logging library

The main idea using a 3rd party library is that instead of console, we wrote: log, debug, JL, etc.
Another benefit is that logging can be turned on and off globally through a config file. It is handy for troubleshooting in production environments