In a school project I am running some javascript that are inputted through the console window and run from there. This script manipulates the web page and outputs the results to the console.
PROBLEM: Keep / save these results in a persistant manner that does not go away on browser close, script malfunctioning / page reload, or if possible, pc crash.
I thought of using frameworks such as Log4js or jStorage (jQuery Storage), but as this is not my website I'm manipulating, I can't add code or markup to the page.
Is there any way to do this?
NOTE: It is not critical that I log the results to console, I could send them off somewhere or do something else with them if that makes it more easy to log.
Thanks.
Here's a little function that stores logs into the WebStorage (persists across browser sessions):
console.plog = function () {
var key = Date.now();
var value = JSON.stringify([].slice.call(arguments));
localStorage.setItem(key, value);
console.log.apply(console, arguments);
}
To restore the logs, you'd have to run the following expression:
(function restoreLogs() {
var timestamps = Object.keys(localStorage).sort();
timestamps.forEach(function (ts) {
var logArgs = JSON.parse(localStorage.getItem(ts));
console.log.apply(console, logArgs);
});
}());
If you want to automatically save everything in Chrome Console you have to run Chrome with these options:
--enable-logging --v=1
It'll save the full log of chrome inside its data directory (note that the file will be overwritten on each run). More details here.
Alternative way: install the Sawbuck utility to manage Chrome logs.
Use console.error(arg);
- it will send the console message into the browser's stderr
(I'm pretty sure it will do so in the release builds, too.)
Start your browser from the command line and redirect your stderr
into some file (something along the lines of chrome 2>errlog
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With