304

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.

Hash
  • 4,647
  • 5
  • 21
  • 39
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • 1
    Duplicate: http://stackoverflow.com/questions/7627113/save-the-console-log-in-chrome-to-a-file http://stackoverflow.com/questions/3462648/export-javascript-console-log-from-google-chrome – MichaelS Aug 07 '12 at 15:55
  • Are you trying to save the console.log from the browser for development purposes? It might help if you explained what your end goal is. – travis Aug 07 '12 at 15:57
  • 1
    @MichaelS I didn't find the object in the log file. – Eduard Florinescu Aug 07 '12 at 16:01
  • @travis I want to export an object to JSON, but all the hierarchy, also his properties and the properties of his properties. I want practically to get the "interface" of an object except the implementation of the functions. – Eduard Florinescu Aug 07 '12 at 16:07
  • 3
    @MichaelS, those questions are about saving the entire log, this question is about saving a single object. They are distinct from my point of view. – James McMahon Feb 12 '13 at 20:27
  • Possible duplicate http://stackoverflow.com/questions/3462648/export-javascript-console-log-from-google-chrome or http://stackoverflow.com/questions/7627113/save-the-console-log-in-chrome-to-a-file – Aaron Blenkush Mar 14 '13 at 04:41
  • A lot of good answers but why not just use JSON.stringify(your_variable) ? Then take the contents via copy and paste (remove outer quotes). – user1889992 Apr 06 '17 at 21:08
  • For issues about *"**Uncaught TypeError: Converting circular structure to JSON**"*, maybe [this](https://stackoverflow.com/questions/41032565/how-to-copy-the-objects-from-chrome-console-window) could help. – KtX2SkD Oct 08 '17 at 14:23

10 Answers10

368

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

gotnull
  • 26,454
  • 22
  • 137
  • 203
Patrick
  • 13,872
  • 5
  • 35
  • 53
  • 3
    I'm not from those who drop "Thanks" everywhere until the answer is blocked to prevent from 'thanks'. But thanks. Gonna build an extension. – Léon Pelletier Jun 04 '15 at 02:55
  • 29
    The Save as... feature actually didn't help. It doesn't save the full JSON object (in my case, I had an array of objects, the objects properties weren't exported in the output file). But hopefully, the good old devtool-snippet you pasted worked like a charm. Thank you – M. Kejji Jun 30 '16 at 13:19
  • 1
    if save as didnt work, then it is a regression. you should file a bug at crbug.com – Patrick Jul 01 '16 at 17:20
  • great! I was missing the the square brackets around the data parameter in: `var blob = new Blob([data], {type: 'text/json'})` – RichardJohnn Jul 12 '16 at 17:46
  • Doesn't work. I get ```download console:1 undefined``` where 'download console' is the name of the snippet – ishandutta2007 Jun 04 '17 at 17:34
  • 1
    @ishandutta2007 you shouldnt be downloading any snippet - it is built into the console now. – Patrick Jun 07 '17 at 08:58
  • @patrick ignore my comment. I didn't know how to run devtools snippet. I was assuming it will automatically run with page load. – ishandutta2007 Jun 07 '17 at 10:25
  • 20
    Right click will not perform a deep save of the object. – user1032531 Sep 28 '17 at 13:41
  • 'Save as' only saves the console to a log file, it won't reveal the property values of the object you console.logged which obviously is what is asked for. This should not be considered the answer, – Mattias Örtenblad Sep 09 '19 at 08:08
  • This doesn't really work it just saves what you see in the console verbatim. For larger objects usually this is just a bunch of ellipses... – Jason C Oct 02 '20 at 19:44
  • also link is broken – joshp Feb 04 '21 at 23:13
  • 1
    Right click on the json data part and save it as global variable then right click on the global variable data in the console and copy-paste it. – smrf May 30 '21 at 12:27
  • @seyedrezafar your hint helped me save the `navigator` var value to a console log dump file (not json but still useful). – Eugene Gr. Philippov Oct 17 '21 at 09:44
  • 1
    Thanks, this helped me scrape my ChatGPT conversations. – David Jan 27 '23 at 09:21
319

UPDATE (06/2021):

Google added a menu action to copy objects. Right click on the object and then click Copy object

enter image description here

OLD ANSWER:

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
artemnih
  • 4,136
  • 2
  • 18
  • 28
141

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.

Martin Burch
  • 2,726
  • 4
  • 31
  • 59
bthota
  • 1,519
  • 1
  • 9
  • 3
  • Note: **you can use `require("util").format(...)`** instead of applying `JSON.stringify()` one by one. [The `util` module on NPM](https://www.npmjs.com/package/util) works on both Node.js and web browsers. – Константин Ван Oct 03 '16 at 02:23
  • 4
    If you type copy(object) and it returned 'undefined', that is actually success. The object is now in your clipboard, and can be pasted. – Dean Feb 01 '18 at 21:14
8

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.

inorganik
  • 24,255
  • 17
  • 90
  • 114
4

This is really late to the party, but maybe it will help someone. My solution seems similar to what the OP described as problematic, but maybe it's a feature that Chrome offers now, but not then. I tried right-clicking and saving the .log file after the object was written to the console, but all that gave me was a text file with this:

console.js:230 Done: Array(50000)[0 … 9999][10000 … 19999][20000 … 29999][30000 … 39999][40000 … 49999]length: 50000__proto__: Array(0)

which was of no use to anyone.

What I ended up doing was finding the console.log(data) in the code, dropped a breakpoint on it and then typed JSON.Stringify(data) in the console which displayed the entire object as a JSON string and the Chrome console actually gives you a button to copy it. Then paste into a text editor and there's your JSON

enter image description here

Adam Hey
  • 1,512
  • 1
  • 20
  • 24
3

There is another open-source tool that allows you to save all console.log output in a file on your server - JS LogFlush (plug!).

JS LogFlush is an integrated JavaScript logging solution which include:

  • cross-browser UI-less replacement of console.log - on client side.
  • log storage system - on server side.

Demo

hindmost
  • 7,125
  • 3
  • 27
  • 39
2

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.

Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
2

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

Akhil_S
  • 39
  • 1
2

Right click on object and saving was not available for me.

The working solution for me is given below

Log as pretty string shown in this answer

console.log('jsonListBeauty', JSON.stringify(jsonList, null, 2));

in Chrome DevTools, Log shows as below

saveJsonlog

Just press Copy, It will be copied to clipboard with desired spacing level

Paste it on your favorite text editor and save it


image took on 15/02/2021, Google Chrome Version 88.0.4324.150

majurageerthan
  • 2,169
  • 3
  • 17
  • 30
0

A more simple way is to use fire fox dev tools, console.log(yourObject) -> right click on object -> select "copy object" -> paste results into notepad

thanks.