javascript - Intercepting web browser console messages - Stack Overflow

I'm working on a web app that's going to be rolling out to a few users for testing. There

I'm working on a web app that's going to be rolling out to a few users for testing. There's obviously going to be bugs, so I'd like to capture them to make it easier to develop fixes. Is there a way for me to intercept any console messages (both browser/js errors and messages generated with console.log) in my web app, so I can send them to a logging service on the server and have them available for debugging?

I'm working on a web app that's going to be rolling out to a few users for testing. There's obviously going to be bugs, so I'd like to capture them to make it easier to develop fixes. Is there a way for me to intercept any console messages (both browser/js errors and messages generated with console.log) in my web app, so I can send them to a logging service on the server and have them available for debugging?

Share Improve this question edited Feb 9, 2013 at 1:47 ajsharma 1,1881 gold badge10 silver badges17 bronze badges asked Jul 26, 2011 at 18:49 sslepiansslepian 1,9215 gold badges21 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Yes.

Intercepting console.log() calls:

console['log'] = function(msg){
    // do wahtever you need with msg here
}

Intercepting errors (so called diaper anti-pattern):

try {
    // your app's code
} catch(err) {
    // do what you to do in case of error need here
}

Here is the proof: jsfiddle.

However, I would suggest creating your own function, that will handle console.log() purpose and could be switched off on production. Plus, it will work properly (that means: won't throw errors), when the browser does not support console.log() calls. This could look like that:

window['log'] = function(msg){
    if (typeof console != 'undefined' && typeof console.log != 'undefined'){
        console.log(msg);
    }
}

A little late... this may be an interesting read, just need to implement the method to send to the server, but that's trivial. Taking Over console.log

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

相关推荐

  • javascript - Intercepting web browser console messages - Stack Overflow

    I'm working on a web app that's going to be rolling out to a few users for testing. There

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信