1

I'm trying to create a logging proxy where each log statement has a prefix.

What I want is to do:

customDebug("xxx","yyy");

and under the wood it will do:

console.debug("prefix","xxx","yyy");

I've tried to implement it this way:

prefixLogArguments: function(arg) {
    var array = _.toArray(arg);
    array.unshift( this.getPrefix() );
    return array;
},

customDebug: function() {
    var argArray = this.prefixLogArguments(arguments);
    console.debug.apply(undefined, argArray );
},

It says Uncaught TypeError: Illegal invocation because it seems we can't invoke native code with apply/call, even with an undefined context.

Can someone tell me how to achieve this?

I can do console.debug(argArray); it's not so bad but it does not produce the same result as it logs an array instead of logging an argument list.

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • possible duplicate of [typeerror illegal invocation on console.log.apply](http://stackoverflow.com/questions/8159233/typeerror-illegal-invocation-on-console-log-apply) – epascarello Jan 22 '14 at 14:53

2 Answers2

5

You should give 'console' as context of apply :

var customDebug = function() {
    console.debug.apply(console, arguments);
};

See : http://jsfiddle.net/X6Sn9

Mickael
  • 5,711
  • 2
  • 26
  • 22
4

Try this:

console.debug.apply(console, argArray);
martinczerwi
  • 2,837
  • 23
  • 23