How to save the output of a console.log(object) to a file?

How to save the output of a console.log(object) to a file?

I tried using JSON.stringify(object), but it doesn’t go down on the whole structure and hierarchy.
On the other hand console.log(object) does that but I cannot save it.
In the console.log output I can expand one by one all the children and select and copy/paste but the structure is to big for that.

Solutions/Answers:

Solution 1:

Update:
You can now just right click

Right click > Save as in the Console panel to save the logged messages to a file.

Original Answer:

You can use this devtools snippet shown below to create a console.save method. It creates a FileBlob from the input, and then automatically downloads it.

(function(console){

console.save = function(data, filename){

    if(!data) {
        console.error('Console.save: No data')
        return;
    }

    if(!filename) filename = 'console.json'

    if(typeof data === "object"){
        data = JSON.stringify(data, undefined, 4)
    }

    var blob = new Blob([data], {type: 'text/json'}),
        e    = document.createEvent('MouseEvents'),
        a    = document.createElement('a')

    a.download = filename
    a.href = window.URL.createObjectURL(blob)
    a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    a.dispatchEvent(e)
 }
})(console)

Source: http://bgrins.github.io/devtools-snippets/#console-save

Related:  How to add a twitter bootstrap tooltip to an icon

Solution 2:

In case you have an object logged:

  • Right click on the object in console and click Store as a global variable
  • the output will be something like temp1
  • type in console copy(temp1)
  • paste to your favorite text editor

Solution 3:

You can use the Chrome DevTools Utilities API copy() command for copying the string representation of the specified object to the clipboard.

If you have lots of objects then you can actually JSON.stringify() all your objects and keep on appending them to a string. Now use copy() method to copy the complete string to clipboard.

Solution 4:

There is an open-source javascript plugin that does just that – debugout.js

Debugout.js records and save console.logs so your application can access them. Full disclosure, I wrote it. It formats different types appropriately, can handle nested objects and arrays, and can optionally put a timestamp next to each log. It also toggles live-logging in one place.

Related:  Is $(document).ready() also CSS ready?

Solution 5:

right click on console.. click save as.. its this simple.. you’ll get an output text file

Solution 6:

You can use library l2i (https://github.com/seriyvolk83/logs2indexeddb) to save all you put into console.log
and then invoke

l2i.download();

to download a file with logs.