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 badges2 Answers
Reset to default 5Yes.
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
评论列表(0条)