Custom console.log that stores line number

I am trying to add some prefix for log output, but it doesn’t work very 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 fine: console.log.bind (console), but I want to add some extra text.

Related topics:

shell console.log that supports line numbers and supports most methods?

+1
source share
1 answer

Using Array.prototype.concat()in a non-array (even as an array) can cause it to add the object itself to the resulting array instead of its contents. Your code also was not actually used [p]in the call concat(), except by simply using it for indirect access to Array.prototype.concat().

Try the following:

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

All Articles