Last Updated: February 25, 2016
·
13.45K
· jmarizgit

JavaScript - quickly hide all console.log

Simple and easy, put this code at the beginning of your code:

console.log = function(){};

All the console messages will magically disappear...

Happy Hacking!

8 Responses
Add your response

I've seen a bunch of these and similar tips for hiding console.log in production but how is console.log getting to production anyway? I've never seen a reason to use console.log over proper breakpoint debugging which requires no code to be turned on/off in production or code at all for that matter. console.log, to me, seems like just a less obtrusive alert()

over 1 year ago ·

@shawncplus has a point, another approach is if you use jslint and CI then make a rule that will ensure that no console.log gets into production (or commited in your main repo).

over 1 year ago ·

@shawncplus I agree for client-side code, but on my NodeJS code this is really useful.

over 1 year ago ·

@shawncplus isn't this for debug mode? I am running in production and don't wanna see the console.log messages from other developers.

over 1 year ago ·

Surely there's a more robust logging solution one could use in production over console.log? That is unless you're implying that you're coding on live.

over 1 year ago ·

That can cause an error if console isn't defined (when IE has dev tools closed). If you wanted to kill it safely, you should do…

var console = {};
console.log = function(){};

Though, really, you probably ought to probably be doing something like this…

https://gist.github.com/nathansmith/5631155

over 1 year ago ·

Bah, somehow GitHub mangled the gist I was trying to link to. Here goes again…

https://gist.github.com/nathansmith/5631193

over 1 year ago ·