简体   繁体   中英

Is there any way to log browser console errors

Suppose a particular user is using Chrome, and gets a runtime error which is logged in Chrome's Console. I'd like to know what that error is. Currently I would have to reach out to the specific user, get them to open up the console and tell me what the error is (or send a screenshot).

Is there a way for me to automatically catch or log that error (regardless of what the error is) and send it to the server?

As a follow-up question is there a way to do this for all major browsers?

You could wrap console.log and console.error with your logging method

var log = console.log;

console.log = function() {
    //Ajax post arguments to your server for logging
    return log.apply(console, arguments);
};

var error = console.error;

console.error = function() {
    //log arguments to server
    return error.apply(console, arguments); 
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM