最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Listening console.log - Stack Overflow

programmeradmin4浏览0评论

I want to set a listener for console.log() and do something with the message without preventing the default behaviour. So, the console of the dev tools should get the message as well. Any ideas?

I want to set a listener for console.log() and do something with the message without preventing the default behaviour. So, the console of the dev tools should get the message as well. Any ideas?

Share Improve this question edited Jun 23, 2011 at 14:58 Greg Guida 7,5124 gold badges31 silver badges40 bronze badges asked Jun 23, 2011 at 14:23 Peter EfransPeter Efrans 1531 gold badge1 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

Never tried this in a webpage, but it work in a browser plugin (where javascripts rights are are not the same for security reasons).

You could definitively go for something like this :

(function(){

    var originallog = console.log;

    console.log = function(txt) {
        // Do really interesting stuff
        alert("I'm doing interesting stuff here !");

        originallog.apply(console, arguments);
    }

})();

The funny thing in javascript is that function are objects too :D

This is a small hack, but I'm not sure there is a better solution:

console._log_old = console.log
console.log = function(msg) {
    alert(msg);
    console._log_old(msg);
}
发布评论

评论列表(0)

  1. 暂无评论