There are a few things that a console.log wrapper can and should do:
- Prevent errors if a console isn’t around (i.e. IE)
- Maintain a history of logs, so you can look in the past if your console is added afterwards (e.g. firebug lite)
- Normalize the browser differences in console integration (e.g. when passing multiple arguments into
- For something you type regularly, make it quicker to type for the lazy among us.
But there are a few considerations…
Console.log.apply doesn’t handle multiple arguments in Safari 3 or Chrome 1.1
Firebug, Chrome, and Safari have a clearer presentation for strings when inside an array:
- A reverse-chronological history, accessible as an array at
- I have
also includeda shorthand
logargs()function that is useful when you’re inside a function and want to know the context and arguments passed in. I use it a lot in ajax callbacks. Worth noting that it uses
arguments.callee.caller, which will be deprecated in ECMAScript 5 Strict mode. :(
1 2 3 4 5 6 7 8 9
And if you’d like it minified:
Interestingly, the minified version of this script is smaller (
262 bytes), and arguably more useful, than the minified firebugx.js, which I’ve covered before. Plus it has one more feature.
Plus Firebug lite?
You got it. This bookmarklet will add firebug lite, and then output the logged history when it’s ready:
>>> Fbug Lite+log <<<
Want more power?After writing this, I worked with Ben Alman on a more comprehensive and robust logging script. It’s excellent if you take full advantage of the console API. And you should be aware that Safari 4 and Chrome 2 have most of that API supported. Make full use of it and don’t you dare type another
2010.07.06: Updated code again. removed logargs. link to pretty viewings in the gist
2011.04.12: Craig Patik expanded the functionality of this in a lot of detail. Great post!