1

I am trying to add some prefix to log output, but it doesn't play well in Chrome:

function getlog(p) {  
  return function() { 
  var mainArguments = [p].concat.call(arguments);
  console.log.bind(console).apply(console, mainArguments); }
}

The simplest solution works great: console.log.bind(console) , but I want to add additional text.

Related topics:

console.log wrapper that keeps line numbers and supports most methods?

NN_
  • 1,593
  • 1
  • 10
  • 26

1 Answers1

0

Using Array.prototype.concat() on a non-array (even an array-like-object), can cause it to add the object itself to the resulting array instead of its contents. Your code was also not actually using [p] in the concat() call other than simply using it to indirectly access Array.prototype.concat().

Try this:

function getlog(p) {  
    return function() { 
        var mainArguments = [p].concat(Array.prototype.slice.call(arguments));
        console.log.apply(console, mainArguments); 
    };
}
JLRishe
  • 99,490
  • 19
  • 131
  • 169