admin管理员组

文章数量:1332389

I want to modify console.log so that it saves everything that the application outputs to the mand 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

I want to modify console.log so that it saves everything that the application outputs to the mand 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
Share Improve this question asked Jun 11, 2015 at 18:44 afonso-botafonso-bot 351 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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

本文标签: javascriptModify consolelogStack Overflow