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

后端 未结 9 1251
情深已故
情深已故 2020-11-28 00:14

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) d

相关标签:
9条回答
  • 2020-11-28 00:40

    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

    0 讨论(0)
  • 2020-11-28 00:43

    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.

    0 讨论(0)
  • 2020-11-28 00:46

    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.

    0 讨论(0)
  • 2020-11-28 00:46

    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.

    0 讨论(0)
  • 2020-11-28 00:48

    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

    0 讨论(0)
  • 2020-11-28 00:52

    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

    0 讨论(0)
提交回复
热议问题