Is there a way to grab everything that has been logged in browser console (network errors, warnings, errors, logs, network errors, etc) and send it to the server ? using Javascript. I have seen a way to intercept a console object's logging functions and collect the data that way, but that doesn't seem to capture all the data that is logged on console. For example network errors (like resource not found). Is there another way ? (Has to work in IE11 and higher, Chrome, FF, Safari)
Asked
Active
Viewed 5,935 times
3
-
2Combine https://stackoverflow.com/questions/7042611/override-console-log-for-production with some ajax... – le_m Jan 23 '18 at 17:38
-
Usually, when you want to collect logs this way, you implement your own logger which both prints the information in the console and sends it to the back-end. – Antony Jan 23 '18 at 17:38
-
1As far as I know, the closest you can get is by hijacking the console functions. So messages cannot be detected by JavaScript. Although you could send a message to the server when, for example, your JavaScript encountered a 404 from a fetch. – Sidney Jan 23 '18 at 17:39
1 Answers
3
Nothing you do will be able to catch every little thing aside from proper testing (perhaps with a headless browser like phantomjs), but this is a start.
function sendStuffToServer(msg){
// do stuff here to send stuff msg to server....
alert(msg);
}
window.onerror = sendStuffToServer;
console.log = sendStuffToServer;
// log some stuff and make an error
console.log("asdf");
asdf()

I wrestled a bear once.
- 22,983
- 19
- 69
- 116