console.log helper function

I have often found it tricky to get a 'just-right' console.log wrapper in JavaScript. I would prefer to just type log, but writing a wrapper function is a little trickier than at first glance. A variation of this used to exist in the HTML5 Boilerplate plugin.js but is now missing.

I have two versions - one is bigger, and is intended for an application - it stubs out console methods when they don't exist, and exposes a log function to the window. The second is portable - it is meant for use inside of plugins.

The neat thing about this technique is that this preserves the original line of code inside of the console.log statement, rather than the line in which the log function is declared.

The code is in a gist.

Comments

  1. Tomas Says:

    Hi, great work!

    how can I call console.warn(‘xxx’) function with your script, log(‘XXX’) will always call console.log.

    Thanks

    Tomas

  2. Brian Says:

    Hi, you can replace ‘log’ with ‘warn’ in this block at the end of the gist: https://gist.github.com/bgrins/5108712#file-log-full-js-L26-L33

Comments are closed. Please contact me instead.