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?

2
  • 1
    Why not console.log.apply(console, mainArguments) ? Commented Sep 8, 2014 at 5:33
  • Then I get line number of .apply method and not of the caller. Commented Sep 8, 2014 at 10:29

1 Answer 1

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); 
    };
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.