将 Chrome 中的 console.log 保存到文件中
有谁知道如何将 Chrome 中的 console.log 输出保存到文件中?或者如何将文本复制出控制台?
假设您正在运行几个小时的功能测试,并且在 Chrome 中获得了数千行 console.log 输出。如何保存或导出?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
好消息
Chrome 开发工具现在允许您将控制台输出本地保存到文件
Chrome 开发者说明 这里。
Good news
Chrome dev tools now allows you to save the console output to a file natively
Chrome Developer instructions here.
我需要做同样的事情,这是我找到的解决方案:
启用使用以下标志从命令行记录:
--启用日志记录--v=1
这会记录 Chrome 内部执行的所有操作,但它也会记录所有
console.log()
消息。该日志文件名为chrome_debug.log
,位于用户数据目录
中,可以通过提供--user- 来覆盖该目录data-dir=PATH
(更多信息)。CONSOLE(\d+)
的行。请注意,控制台日志不会与
--incognito
一起出现。I needed to do the same thing and this is the solution I found:
Enable logging from the command line using the flags:
--enable-logging --v=1
This logs everything Chrome does internally, but it also logs all the
console.log()
messages as well. The log file is calledchrome_debug.log
and is located in theUser Data Directory
which can be overridden by supplying--user-data-dir=PATH
(more info here).CONSOLE(\d+)
.Note that console logs do not appear with
--incognito
.我为此找到了一个很棒且简单的方法。
在控制台中 - 右键单击控制台记录的对象
单击“存储为全局变量”
查看新变量的名称 - 例如,它是variableName1
在控制台中键入:JSON.stringify(variableName1)
复制变量字符串内容:eg {"a":1,"b":2,"c":3}
例如 https://jsoneditoronline.org/
I have found a great and easy way for this.
In the console - right click on the console logged object
Click on 'Store as global variable'
See the name of the new variable - e.g. it is variableName1
Type in the console: JSON.stringify(variableName1)
Copy the variable string content: e.g. {"a":1,"b":2,"c":3}
e.g. https://jsoneditoronline.org/
有一个开源 javascript 插件可以做到这一点,但对于任何浏览器 - debugout.js
Debugout.js 记录和保存 console.logs 以便您的应用程序可以访问它们。完全公开,我写的。它适当地格式化不同的类型,可以处理嵌套对象和数组,并且可以选择在每个日志旁边放置一个时间戳。您还可以在一处切换实时日志记录,而无需删除所有日志记录语句。
There is an open-source javascript plugin that does just that, but for any browser - 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. You can also toggle live-logging in one place, and without having to remove all your logging statements.
为了更好的日志文件(没有 Chrome 调试废话)使用:
而不是
--v=1
这信息太多了。它仍然会提供错误和警告,就像您通常在 Chrome 控制台中看到的那样。
2020 年 5 月 18 日更新:实际上,我认为这不再是事实。无论此日志记录级别如何,我都找不到控制台消息。
For better log file (without the Chrome-debug nonsense) use:
instead of
--v=1
which is just too much info.It will still provide the errors and warnings like you would typically see in the Chrome console.
update May 18, 2020: Actually, I think this is no longer true. I couldn't find the console messages within whatever this logging level is.
这可能有帮助,也可能没有帮助,但在 Windows 上,您可以使用 Windows 事件跟踪读取控制台日志
http://msdn.microsoft.com/en-us/library/ms751538.aspx
我们的集成测试在 .NET 中运行,因此我使用此方法将控制台日志添加到我们的测试输出中。我制作了一个示例控制台项目来演示: https://github.com/jkells/chrome-trace
--enable-logging --v=1 似乎不适用于最新版本的 Chrome。
This may or may not be helpful but on Windows you can read the console log using Event Tracing for Windows
http://msdn.microsoft.com/en-us/library/ms751538.aspx
Our integration tests are run in .NET so I use this method to add the console log to our test output. I've made a sample console project to demonstrate here: https://github.com/jkells/chrome-trace
--enable-logging --v=1 doesn't seem to work on the latest version of Chrome.
有很多好的答案,但为什么不直接使用 JSON.stringify(your_variable) 呢?然后通过复制和粘贴获取内容(删除外部引号)。我也在以下位置发布了相同的答案: 如何将 console.log(object) 的输出保存到文件中?
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). I posted this same answer also at: How to save the output of a console.log(object) to a file?
还有另一个开源工具,它允许您将所有
console.log
输出保存在服务器上的文件中 - JS LogFlush(插件!)。演示
There is another open-source tool which allows you to save all
console.log
output in a file on your server - JS LogFlush (plug!).Demo
如果您在本地主机上运行 Apache 服务器(不要在生产服务器上执行此操作),您还可以将结果发布到脚本,而不是将其写入控制台。
而不是
console.log
因此,您可以编写:然后
save.php
可以执行此操作,If you're running an Apache server on your localhost (don't do this on a production server), you can also post the results to a script instead of writing it to console.
So instead of
console.log
, you can write:Then
save.php
can do thiscopy(temp1)
并按回车键(将temp1
替换为上一步中的变量名称)。现在记录的值已复制到剪贴板。如果您不想在 Chrome 中更改标志/设置并且不想处理 JSON 字符串化和解析等,那么这是一种特别好的方法。
更新:我刚刚找到了我用图像建议的解释,更容易理解 https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html
copy(temp1)
and hit return (replacetemp1
with the variable name from the previous step). Now the logged value is copied to your clipboard.This is especially good as an approach if you don't want to mess with changing flags/settings in Chrome and don't want to deal with JSON stringifying and parsing etc.
Update: I just found this explanation of what I suggested with images that's easier to follow https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html
在 Linux 上(至少),您可以在环境中设置 CHROME_LOG_FILE,让 chrome 在每次运行时将控制台活动的日志写入指定文件。每次 chrome 启动时都会覆盖该日志。这样,如果您有一个运行 chrome 的自动会话,则无需更改 chrome 的启动方式,并且日志会在会话结束后保留。
导出 CHROME_LOG_FILE=chrome.log
On Linux (at least) you can set CHROME_LOG_FILE in the environment to have chrome write a log of the Console activity to the named file each time it runs. The log is overwritten every time chrome starts. This way, if you have an automated session that runs chrome, you don't have a to change the way chrome is started, and the log is there after the session ends.
export CHROME_LOG_FILE=chrome.log
该线程中的其他解决方案在我的 Mac 上不起作用。这是一个使用 ajax 间歇性保存字符串表示形式的记录器。 根据您的需要,将其与
console.save
而不是console.log
一起使用,您可以保存该字符串或仅保存
console.log
并复制并粘贴。如果您想保存它,这里有一个 ajax:saveLog.php
应该将数据附加到日志文件中的某个位置。我不需要该部分,因此我不将其包含在这里。 :)https://www.google.com/search?q= php+附加+到+日志
the other solutions in this thread weren't working on my mac. Here's a logger that saves a string representation intermittently using ajax. use it with
console.save
instead ofconsole.log
depending on what you need, you can save that string or just
console.log
it and copy and paste. here's an ajax for you in case you want to save it:the
saveLog.php
should append the data to a log file somewhere. I didn't need that part so I'm not including it here. :)https://www.google.com/search?q=php+append+to+log
这个答案可能看起来特别相关,但是专门针对网络日志,您可以访问以下链接。
我发布此答案的原因是因为就我而言,
console.log
打印了 长截断文本,因此我无法从控制台获取值。我通过直接从网络日志打印 api 响应来解决这个问题。您可能会看到与此类似的窗口,只需按
开始记录到磁盘
按钮即可:This answer might seem specifically related, but specifically for Network Log, you can visit the following link.
The reason I've post this answer is because in my case, the
console.log
printed a long truncated text so I couldn't get the value from the console. I solved by getting the api response I was printing directly from the network log.There you may see a similar windows to this, just press the
Start Logging to Disk
button and that's it:一种方法是将结果转换为字符串,并将该字符串数据转换为 blob< /a> 然后到 数据 url 并制作可使用
标签和
click()
下载One approch is to convert results into string and convert that string data to blob and then to data url and make it downloadable using
<a>
tag andclick()
使用以下命令创建批处理文件并将其另存为桌面上的 ChromeDebug.bat。
start chrome --enable-logging --v=1
关闭所有其他 Chrome 选项卡和窗口。
双击
ChromeDebug.bat
文件,这将打开 Chrome 并在任务栏中显示带有 Chrome 图标的命令提示符。所有 Web 应用程序日志将存储在以下路径中。
在运行命令中运行以下路径以打开 chrome 日志文件
%LocalAppData%\Google\Chrome\User Data\chrome_debug.log
Create a batch file using below command and save it as ChromeDebug.bat in your desktop.
start chrome --enable-logging --v=1
Close all other Chrome tabs and windows.
Double click
ChromeDebug.bat
file which will open Chrome and a command prompt with Chrome icon in taskbar.All the web application logs will be stored in below path.
Run the below path in Run command to open chrome log file
%LocalAppData%\Google\Chrome\User Data\chrome_debug.log