如何将console.log(object)的输出保存到文件中? [英] How to save the output of a console.log(object) to a file?
问题描述
我尝试过使用 JSON.stringify(object)
,但它并没有在整个结构和层次结构中出现。
另一方面, console.log(object)
这样做,但我无法保存。
在 console.log
输出中,我可以逐个展开所有子项并选择并复制/粘贴,但 更新:
现在您可以右键点击
右键点击>保存为控制台面板将记录的消息保存到文件中。
原始答案:
您可以使用下面显示的devtools片段来创建一个console.save方法。它从输入创建一个FileBlob,然后自动下载它。
(function(console){
console.save =函数(数据,文件名){
if(!data){
console.error('Console.save:No data')
return;
$ b $ 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 =文件名
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,false,false,false,0,null)
a.dispatchEvent(e)
}
})(console)
资料来源: http://bgrins.github.io/devtools- snippets /#console-save
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.
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
这篇关于如何将console.log(object)的输出保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!