Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store console logging in Chrome in a persistent way

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.

like image 611
Erik Wendel Avatar asked Apr 18 '12 12:04

Erik Wendel


3 Answers

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);
    });
}());
like image 160
katspaugh Avatar answered Oct 13 '22 09:10

katspaugh


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.

like image 27
Adriano Repetti Avatar answered Oct 13 '22 11:10

Adriano Repetti


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).

like image 1
Alexander Pavlov Avatar answered Oct 13 '22 11:10

Alexander Pavlov