Naive way of overriding console.log


Happy new year folks. I hope you had a fantastic year and plan to have a great 2015.

I am going to start 2015 with a simple naive trick. I am sure there are other good ways and things out in the wild to achieve similar results.

I was visiting a site for holiday shopping and I notice tons and tons of console logs. I assumed that the site was in debugging mode and had lot of console prints. It got me thinking that what would be the easiest way to get rid of those console logs in production while still keep them in debugging/developing mode. Of course assuming that they have no prior built-in switches for that.

I opened up my fiddle and here is what I came with, It works well and pretty easy to use. Its a simple logger. It uses domain name array which is the array of all the domains in which the console.log should be hidden (example, production domain). It has a force switch which allows you to still print logs in production (in case you are working like me where your end users are in the same room). This is a bare-bone structure to give you the idea – it can obviously be tweaked according to one’s need.

Here is the embedded fiddle

With this, all the console log disappear from production while still appear in everyones local.
Hope this helps someone.

Happy Coding.