Created
June 14, 2022 15:09
-
-
Save petronioamaral/10e43a535cfdb9b2f4dde3504c3573b5 to your computer and use it in GitHub Desktop.
Save output console.log to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function that will call your webserver | |
logToServer = function (consoleMsg) { | |
// only do this if on device | |
// if (window.cordova) { | |
let jsonTxt = customStringify(consoleMsg); | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.open( | |
"GET", | |
"http://wsite.localhost/console2server.php?msg=" + jsonTxt, | |
true | |
); //async | |
xmlHttp.send(null); | |
// } | |
}; | |
// Test if you receive this on the server | |
// intercept console logs | |
(function () { | |
var oldLog = console.log; | |
console.log = function (message) { | |
// DO MESSAGE HERE. | |
logToServer(message); | |
oldLog.apply(console, arguments); | |
}; | |
})(); | |
// intecept errors | |
if (window && !window.onerror) { | |
window.onerror = function (errorMsg, url, lineNumber, column, errorObj) { | |
logToServer(errorMsg); | |
logToServer(errorObj); | |
return false; | |
}; | |
} | |
// this is optional, but it avoids 'converting circular structure' errors | |
customStringify = function (inp) { | |
return JSON.stringify(inp, function (key, value) { | |
if (typeof value === "object" && value !== null) { | |
if (cache.indexOf(value) !== -1) { | |
// Circular reference found, discard key | |
console.log("circular dep found!!"); | |
return; | |
} | |
// Store value in our collection | |
cache.push(value); | |
} | |
return value; | |
}); | |
}; | |
<?php | |
//allow CORS request | |
header('Access-Control-Allow-Origin: *'); | |
$fp = fopen('/home/logs.log', 'a'); | |
if(isset($_GET['msg'])) { | |
//you can also log to a file or whatever, I just log to standard logs | |
// error_log("[CONSOLE.LOG] ".json_decode($_GET['msg'], true), "/home/git/logs.log"); | |
fwrite($fp, "\n".json_decode($_GET['msg'], true)); | |
} | |
fclose($fp); | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment