Javascript 如何将 console.log(object) 的输出保存到文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11849562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:22:21  来源:igfitidea点击:

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

javascriptjsongoogle-chrome

提问by Eduard Florinescu

I tried using JSON.stringify(object), but it doesn't go down on the whole structure and hierarchy.

我尝试使用JSON.stringify(object),但它不会影响整个结构和层次结构。

On the other hand console.log(object)does that but I cannot save it.

另一方面console.log(object),这样做但我无法保存它。

In the console.logoutput I can expand one by one all the children and select and copy/paste but the structure is to big for that.

console.log输出中,我可以一一展开所有子项并选择和复制/粘贴,但结构太大了。

回答by Patrick

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.

您可以使用下面显示的这个 devtools 片段来创建一个 console.save 方法。它从输入创建一个 FileBlob,然后自动下载它。

(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

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

回答by artemdev

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
  • 右键单击控制台中的对象,然后单击 Store as a global variable
  • 输出将类似于 temp1
  • 在控制台输入 copy(temp1)
  • 粘贴到您最喜欢的文本编辑器

回答by bthota

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

您可以使用Chrome DevTools Utilities APIcopy()命令将指定对象的字符串表示复制到剪贴板。

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.

如果您有很多对象,那么您实际上可以将所有对象 JSON.stringify() 并继续将它们附加到字符串中。现在使用copy()方法将完整的字符串复制到剪贴板。

回答by inorganik

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

有一个开源 javascript 插件可以做到这一点 - 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.

Debugout.js 记录并保存 console.logs,以便您的应用程序可以访问它们。完全公开,我写了。它适当地格式化不同的类型,可以处理嵌套的对象和数组,并且可以选择在每个日志旁边放置一个时间戳。它还可以在一处切换实时日志记录。

回答by Akhil_S

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

右键单击控制台.. 单击另存为.. 就这么简单.. 你会得到一个输出文本文件

回答by Alexander Volkov

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

您可以使用库l2ihttps://github.com/seriyvolk83/logs2indexeddb)来保存您放入的所有内容console.log然后调用

l2i.download();

to download a file with logs.

下载带有日志的文件。

回答by hindmost

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

还有另一个开源工具允许您将所有console.log输出保存在服务器上的文件中 - JS LogFlush(插件!)。

JS LogFlushis an integrated JavaScript logging solution which include:

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

JS LogFlush是一个集成的 JavaScript 日志解决方案,其中包括:

  • 跨浏览器 UI-less 替换 console.log - 在客户端。
  • 日志存储系统 - 在服务器端。

Demo

演示

回答by Adam Hey

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:

这对派对来说真的很晚,但也许它会帮助某人。我的解决方案似乎与 OP 描述的有问题的解决方案相似,但也许这是 Chrome 现在提供的功能,但不是那时。在将对象写入控制台后,我尝试右键单击并保存 .log 文件,但给我的只是一个带有以下内容的文本文件:

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

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

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

我最终做的是console.log(data)在代码中找到 ,在其上放置一个断点,然后JSON.Stringify(data)在控制台中键入,该控制台将整个对象显示为 JSON 字符串,Chrome 控制台实际上为您提供了一个复制它的按钮。然后粘贴到文本编辑器中,这是您的 JSON

enter image description here

enter image description here