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

10

307

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.

Risotto answered 7/8, 2012 at 15:48 Comment(8)
Duplicate: #7627613 #3463148Guessrope
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.Chobot
@Guessrope I didn't find the object in the log file.Risotto
@Chobot 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.Risotto
@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.Linchpin
Possible duplicate #3463148 or #7627613Dallis
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).Heavily
For issues about "Uncaught TypeError: Converting circular structure to JSON", maybe this could help.Mousy
D
373

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

Duomo answered 6/11, 2013 at 17:25 Comment(14)
I'm not from those who drop "Thanks" everywhere until the answer is blocked to prevent from 'thanks'. But thanks. Gonna build an extension.Purificator
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 youHeteromerous
if save as didnt work, then it is a regression. you should file a bug at crbug.comDuomo
great! I was missing the the square brackets around the data parameter in: var blob = new Blob([data], {type: 'text/json'})Rainband
Doesn't work. I get download console:1 undefined where 'download console' is the name of the snippetAdamec
@Adamec you shouldnt be downloading any snippet - it is built into the console now.Duomo
@patrick ignore my comment. I didn't know how to run devtools snippet. I was assuming it will automatically run with page load.Adamec
Right click will not perform a deep save of the object.Loving
'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,Etymon
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...Musclebound
also link is brokenCanasta
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.Regine
@seyedrezafar your hint helped me save the navigator var value to a console log dump file (not json but still useful).Gwenette
Thanks, this helped me scrape my ChatGPT conversations.Gregoire
R
320

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
Raasch answered 18/11, 2015 at 1:27 Comment(8)
I also found that pasting this into konklone.io/json you can then quickly get this into a CSV file and from there into Excel.Hames
I only get [object Object]Decrement
The console says "undefined", but that does not mean if failed. It still copies it to the clipboard:)Eaten
This is the answer that worked for me, but maybe it should be mentioned that the specific context has to be specified (e.g. when using iFrames)Purehearted
The console returns undefined because the copy method doesn't have a return :)Oaks
By far the simplest and most reliable solution!Sherrard
thank you! definitely best solution as approved google+ url is deadBibber
Are the first two steps still necessary? I could just do 'copy(this.myVariable)'.Shortly
T
142

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.

Taco answered 5/6, 2013 at 0:48 Comment(2)
Note: you can use require("util").format(...) instead of applying JSON.stringify() one by one. The util module on NPM works on both Node.js and web browsers.Gastronomy
If you type copy(object) and it returned 'undefined', that is actually success. The object is now in your clipboard, and can be pasted.Eaten
T
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.

Tablecloth answered 17/6, 2014 at 18:0 Comment(2)
I'm getting an error - SyntaxError: export declarations may only appear at top level of a module --> debugout.js:9Chemarin
@SenuraDissanayake try now - i had to revert someone's PR that I didn't test :/Tablecloth
B
5

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

Briefless answered 28/11, 2019 at 13:57 Comment(2)
it says at the end long text was truncated, if your press Copy does it copy the entire 20.6MB?Risotto
@EduardFlorinescu yes, everythingBriefless
K
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

Karyokinesis answered 15/9, 2014 at 21:38 Comment(0)
S
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.

Scotticism answered 2/7, 2014 at 19:41 Comment(0)
S
2

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

Sledge answered 3/5, 2015 at 9:16 Comment(1)
just without any indication of what is error vs waning vs log.Stu
N
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

Neolamarckism answered 15/2, 2021 at 12:18 Comment(0)
T
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.

Twinkling answered 16/7, 2020 at 0:39 Comment(1)
This option already appears to have been covered by the accepted answer already. Also, the question is tagged for Google Chrome.Unfrock

© 2022 - 2024 — McMap. All rights reserved.