javascript - Modify console.log - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742289379a4415907.html

相关推荐

  • javascript - Modify console.log - Stack Overflow

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

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信