Home How to remove JavaScript console.log - pros&cons
Post
Cancel

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:

  • grunt task to remove console.* statements.
  • UglifyJS 2 drop_console option to discard console.* functions

and some more.

Pros

  • console.* statements are removed from our codebase
  • it is done automatically

Cons

  • only way turn logging is to rebuild&deploy our code
  • we have to check if library supports fancy statements like console && console.log("foo")

Method 3: Using a logging library

There are a couple libraries available on market. Just few names:

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

Pros

  • libraries are configurable, so we can disable only some logs

Cons

  • used plugins probably does not use same logging library
  • we need to run periodic check to see if nobody is using console.*

Method 4: Override

This approach allows us to override the console.* statements. And, we can easily do it globally. The full code is in my post: Disable JavaScript console on production

Pros

  • it’s easy to implement
  • it includes all external libraries logs statements
  • it is configurable. In my code I disable all log types, but we can choose to disable only a subset (only error or info)

Cons

  • does anybody have an idea?

If you have another idea on how to implement this or want to discuss this further feel free to leave a comment or send me a message.

This post is licensed under CC BY 4.0 by the author.