Create shortcut to console.log() in Chrome

pimvdb picture pimvdb · Mar 28, 2011 · Viewed 9.2k times · Source

Because I'm lazy, I created a function log that basically is just an abbreviation of console.log:

function log() {
  console.log.apply(console, arguments);
}

Whenever I call this, I see the logged item in Google Chrome's Developer Tools, with on the right hand side the line number where the item was logged. However, this line number is always the same, because the actual console.log call is located at one specific place in the code (namely where I declare the log function as above).

What I also tried is just:

var log = console.log;

but this always throws an error: Illegal invocation. Weird, but I guess that's not a possibility.

How can I make a shortcut to console.log, with Developer Tools showing the line number where log was called, rather than where the actual console.log call is located?

Answer

pimvdb picture pimvdb · Mar 29, 2011

When I reported it, it was refused but the answer was simple - create the shortcut like this:

var log = console.log.bind(console);

This doesn't leave out the line number, whilst you can call it like log(...).