To address your query, you will need to follow two steps:
- Extract the console.log output and store it in a variable
- Save the variable content to a text file
Step 1 - Monitoring Console Logs
You can achieve this by creating a custom interface to replace the console object:
var consoleText = ""
// Create a new console
window.console = (function(console){
return {
log: function(text){
console.log(text);
consoleText += text;
},
info: function (text) {
console.info(text);
consoleText += text;
},
warn: function (text) {
console.warn(text);
consoleText += text;
},
error: function (text) {
console.error(text);
consoleText += text;
}
};
}(window.console));
To handle uncaught exceptions:
window.addEventListener('error', function(event) {
consoleText += event.message;
})
Step 2 - Saving the Log File
Following DevonTaig's solution on Writing html form data to a txt file without the use of a webserver:
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
download("log.txt", consoleText)