Joined May 2011
·

Nathan Smith

Principal UI Architect at projekt202.com
·
Dallas, TX
·
·
·

Posted to Sensible console.log over 1 year ago

@solocommand

By the way, Paul Irish just pointed me in this direction. Seems to be a very robust solution (leaves line-numbers intact, for the origin of the log)…

https://gist.github.com/bgrins/5108712

Posted to Sensible console.log over 1 year ago

Yeah, I hear ya. Can't account for what others (3rd parties) might do.

Might I suggest (so you can call it from within whatever scope)…

window.console = window.console || {log:function(){}};

That way you can have it nested inside something else, if need be.

In terms of cut-and-paste handiness, that makes sure you're not creating a local var console somewhere.

Anyway, that's super nitpicky, I know.

:)

Posted to Sensible console.log over 1 year ago

@solocommand

Yes, my approach would require developers to use the log() function instead of console.log.

However, I was intrigued by your suggestion. So, I just threw this together.

Sadly, it doesn't work…

(function(window) {
  window.console = window.console || {};
  var log = window.console.log || function(){};
  var host = window.location.hostname;
  var debug = host.match(/localhost|127.0.0.1|staging.example.com/);

  // debug = true;

  window.console.log = function() {
    if !(debug) {
      return;
    }

    for (var i = 0, ii = arguments.length; i < ii; i++) {
      log(arguments[i]);
    }
  };
})(this);

It appears that while you can override window.console — which is seemingly just a pointer to an internal object, the actual console.log function is native to the browser and does not want to be overridden.

I get this error…

http://cl.ly/image/081T1H2C023A

"Illegal invocation."

Note the bit about [native code].

Anyway, I don't think it's possible to repurpose console.log itself.

Note: It is possible to edit things that live at the global scope. For instance, here's an example of how to force parseInt to use a base-10 radix…

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

So, I don't think the approach is the problem. I guess that console.log is just impervious to tampering?

Posted to Sensible console.log over 1 year ago

@solocommand

That would fix the issue of IE throwing errors when the dev tools are closed. However, it wouldn't prevent console.log messages from being emitted to anyone/everyone with their browser's dev tools open.

Your log messages would still be seen by anyone using the dev tools in Chrome, Safari, Firefox, Opera, IE, etc.

That is, unless you're stripping out console.log from your code before deployment. In which case, defining console as a variable isn't actually necessary.

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

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

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

Achievements
178 Karma
2,407 Total ProTip Views