afonso-bot
afonso-bot

Reputation: 35

Modify console.log

I want to modify console.log so that it saves everything that the application outputs to the command line using console.log.

I have tried

var log = console.log;

console.log = function () {
    // fs.appendFile('log.txt ..
    log.apply(log, arguments);
}

But it gives me the error

 Illegal invocation

Upvotes: 1

Views: 4080

Answers (1)

Felix Kling
Felix Kling

Reputation: 816232

The first arguments to apply is what this will refer to. To mimic a call to console.log(), you have to pass console, not the function itself:

 var log = console.log;

 log.apply(console, arguments);
 // log.apply(this, arguments); would work as well

Upvotes: 10

Related Questions