28

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.

François Beaufort
  • 4,843
  • 3
  • 29
  • 38
  • http://www.whathaveyoutried.com/? Should this run on a website or as a browser plugin? What is your target browser? Please add this information to your question/tags. – rekire Dec 07 '12 at 12:46
  • Give a detailed explaino of what you are trying to do. Only then can we help you . – The Dark Knight Dec 07 '12 at 13:05

5 Answers5

16

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"
  }
}
5

Here is a way to get chrome's console history (not a javascript way though) -

  1. While in DevTools, Undock into separate window (search for it with Ctrl+Shift+P)
  2. Ctrl+Shift+J to DevTool your DevTools
  3. Go to the Application tab -> Local Storage -> devtools://devtools
  4. Double-click or Edit the value of consoleHistory, and copy it

Ref: https://chema.medio.click/en/dev/reviewing-the-console-command-history-in-chromes-devtools/

Sunny Tambi
  • 2,393
  • 3
  • 23
  • 27
4

Chrome extensions had an API for that, experimental.devtools.console:

chrome.experimental.devtools.console.getMessages(function(messages) {  })

This API has been removed.

NVI
  • 14,907
  • 16
  • 65
  • 104
2

There is no way to get the console data with JavaScript. Only way you would be able to do it is basically hijack all the console functions and store a copy and than call the default log lines.

epascarello
  • 204,599
  • 20
  • 195
  • 236
2
console.history = [];
var oldConsole = {};
for (var i in console) {
    if (typeof console[i] == 'function') {
        oldConsole[i] = console[i];
        var strr = '(function(){\
            console.history.push({func:\'' + i + '\',args : Array.prototype.slice.call(arguments)});\
            oldConsole[\'' + i + '\'].apply(console, arguments);\
        })';
        console[i] = eval(strr);
    }
}

And then use console.history to access history

Séverin
  • 137
  • 1
  • 6