Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get console history

Tags:

I'd like to know if there is a way in javascript to retrieve console history.

What I mean by console history is what appears in the dev tools console. For instance, I'd like to print in a html page all errors, warnings, info and log that are displayed in my dev tools without opening them.

Let me know if I'm not clear.

like image 849
François Beaufort Avatar asked Dec 07 '12 12:12

François Beaufort


1 Answers

I wrote a simple cross-browser library for this, called console.history. It's available on GitHub: https://git.io/console

What the library basically does is catch all calls to console.[log/warn/error/debug/info] and store them in the console.history array. As a bonus, a full stack trace is also added.

Test file test.js contains:

function outer() {
  inner();
}

function inner() {
  var array = [1,2,3];
  var object = {"foo": "bar", "key": "value"};
  console.warn("Something went wrong, but we're okay!", array, object);
}

outer();

The entry to console.history will be:

{
  "type": "warn",
  "timestamp": "Thu, 01 Sep 2016 15:38:28 GMT",
  "arguments": {
    "0": "Something went wrong, but we're okay!",
    "1": [1, 2, 3],
    "2": {
      "foo": "bar",
      "key": "value"
    }
  },
  "stack": {
    "0": "at inner (http://localhost:1337/test/test.js:6:11)",
    "1": "at outer (http://localhost:1337/test/test.js:2:3)",
    "2": "at http://localhost:1337/test/test.js:9:1"
  }
}
like image 90
Sander Avatar answered Sep 19 '22 07:09

Sander