如何将 console.log(object) 的输出保存到文件中? [英] How to save the output of a console.log(object) to a file?
问题描述
我尝试使用 JSON.stringify(object)
,但它并没有影响整个结构和层次结构.
I tried using JSON.stringify(object)
, but it doesn't go down on the whole structure and hierarchy.
另一方面,console.log(object)
会这样做,但我无法保存它.
On the other hand console.log(object)
does that but I cannot save it.
在 console.log
输出中,我可以一一展开所有子项并选择和复制/粘贴,但结构太大了.
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.
推荐答案
更新:您现在只需右键单击
右键单击 > 在控制台面板中另存为将记录的消息保存到文件中.
Right click > Save as in the Console panel to save the logged messages to a file.
原答案:
您可以使用下面显示的这个 devtools 片段来创建一个 console.save 方法.它从输入创建一个 FileBlob,然后自动下载它.
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)
来源:http://bgrins.github.io/devtools-snippets/#console-save一个>
这篇关于如何将 console.log(object) 的输出保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!