将 Chrome 中的 console.log 保存到文件中

发布于 2024-12-07 19:12:49 字数 128 浏览 1 评论 0 原文

有谁知道如何将 Chrome 中的 console.log 输出保存到文件中?或者如何将文本复制出控制台?

假设您正在运行几个小时的功能测试,并且在 Chrome 中获得了数千行 console.log 输出。如何保存或导出?

Does anyone know of a way to save the console.log output in Chrome to a file? Or how to copy the text out of the console?

Say you are running a few hours of functional tests and you've got thousands of lines of console.log output in Chrome. How do you save it or export it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

冰雪之触 2024-12-14 19:12:49

好消息

Chrome 开发工具现在允许您将控制台输出本地保存到文件

  1. 打开控制台
  2. 右键单击
  3. 选择“另存为..”

Chrome 开发者说明 这里

Good news

Chrome dev tools now allows you to save the console output to a file natively

  1. Open the console
  2. Right-click
  3. Select "save as.."

save console to file

Chrome Developer instructions here.

梦醒时光 2024-12-14 19:12:49

我需要做同样的事情,这是我找到的解决方案:

  1. 启用使用以下标志从命令行记录

    --启用日志记录--v=1

这会记录 Chrome 内部执行的所有操作,但它也会记录所有 console.log() 消息。该日志文件名为 chrome_debug.log,位于 用户数据目录 中,可以通过提供 --user- 来覆盖该目录data-dir=PATH更多信息)。

  1. 过滤您获得的日志文件中包含 CONSOLE(\d+) 的行。

请注意,控制台日志不会与 --incognito 一起出现。

I needed to do the same thing and this is the solution I found:

  1. 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 called chrome_debug.log and is located in the User Data Directory which can be overridden by supplying --user-data-dir=PATH (more info here).

  1. Filter the log file you get for lines with CONSOLE(\d+).

Note that console logs do not appear with --incognito.

戴着白色围巾的女孩 2024-12-14 19:12:49

我为此找到了一个很棒且简单的方法。

  1. 在控制台中 - 右键单击​​控制台记录的对象

  2. 单击“存储为全局变量”

  3. 查看新变量的名称 - 例如,它是variableName1

  4. 在控制台中键入:JSON.stringify(variableName1)

  5. 复制变量字符串内容:eg {"a":1,"b":2,"c":3}

在此处输入图像描述

  1. 转到某个 JSON 在线编辑器:
    例如 https://jsoneditoronline.org/

在此处输入图像描述

I have found a great and easy way for this.

  1. In the console - right click on the console logged object

  2. Click on 'Store as global variable'

  3. See the name of the new variable - e.g. it is variableName1

  4. Type in the console: JSON.stringify(variableName1)

  5. Copy the variable string content: e.g. {"a":1,"b":2,"c":3}

enter image description here

  1. Go to some JSON online editor:
    e.g. https://jsoneditoronline.org/

enter image description here

笑脸一如从前 2024-12-14 19:12:49

有一个开源 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.

雨夜星沙 2024-12-14 19:12:49

为了更好的日志文件(没有 Chrome 调试废话)使用:

--enable-logging --log-level=0

而不是
--v=1 这信息太多了。

它仍然会提供错误和警告,就像您通常在 Chrome 控制台中看到的那样。

2020 年 5 月 18 日更新:实际上,我认为这不再是事实。无论此日志记录级别如何,我都找不到控制台消息。

For better log file (without the Chrome-debug nonsense) use:

--enable-logging --log-level=0

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.

痴情 2024-12-14 19:12:49

这可能有帮助,也可能没有帮助,但在 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.

带上头具痛哭 2024-12-14 19:12:49

有很多好的答案,但为什么不直接使用 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?

禾厶谷欠 2024-12-14 19:12:49

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

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

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

演示

There is another open-source tool which 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

牛↙奶布丁 2024-12-14 19:12:49

如果您在本地主机上运行 Apache 服务器(不要在生产服务器上执行此操作),您还可以将结果发布到脚本,而不是将其写入控制台。

而不是 console.log

JSONP('http://localhost/save.php', {fn: 'filename.txt', data: json});

因此,您可以编写:然后 save.php 可以执行此操作,

<?php

 $fn = $_REQUEST['fn'];
 $data = $_REQUEST['data'];

 file_put_contents("path/$fn", $data);

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:

JSONP('http://localhost/save.php', {fn: 'filename.txt', data: json});

Then save.php can do this

<?php

 $fn = $_REQUEST['fn'];
 $data = $_REQUEST['data'];

 file_put_contents("path/$fn", $data);
绿阴红影里的.如风往事 2024-12-14 19:12:49
  1. 直接右键单击要复制的记录值
  2. 在右键单击菜单中,选择“存储为全局变量”
  3. 您将在控制台的下一行看到保存为类似“temp1”的值 在控制台中
  4. ,输入 copy(temp1) 并按回车键(将 temp1 替换为上一步中的变量名称)。现在记录的值已复制到剪贴板。
  5. 将值粘贴到您想要的任何位置

如果您不想在 Chrome 中更改标志/设置并且不想处理 JSON 字符串化和解析等,那么这是一种特别好的方法。

更新:我刚刚找到了我用图像建议的解释,更容易理解 https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html

  1. Right-click directly on the logged value you want to copy
  2. In the right-click menu, select "Store as global variable"
  3. You'll see the value saved as something like "temp1" on the next line in the console
  4. In the console, type copy(temp1) and hit return (replace temp1 with the variable name from the previous step). Now the logged value is copied to your clipboard.
  5. Paste the values to wherever you want

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

相权↑美人 2024-12-14 19:12:49

在 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

弱骨蛰伏 2024-12-14 19:12:49

该线程中的其他解决方案在我的 Mac 上不起作用。这是一个使用 ajax 间歇性保存字符串表示形式的记录器。 根据您的需要,将其与 console.save 而不是 console.log 一起使用

var logFileString="";
var maxLogLength=1024*128;

console.save=function(){
  var logArgs={};

  for(var i=0; i<arguments.length; i++) logArgs['arg'+i]=arguments[i];
  console.log(logArgs);

  // keep a string representation of every log
  logFileString+=JSON.stringify(logArgs,null,2)+'\n';

  // save the string representation when it gets big
  if(logFileString.length>maxLogLength){
    // send a copy in case race conditions change it mid-save
    saveLog(logFileString);
    logFileString="";
  }
};

,您可以保存该字符串或仅保存 console.log 并复制并粘贴。如果您想保存它,这里有一个 ajax:

function saveLog(data){
  // do some ajax stuff with data.
  var xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200) {}
  }

  xhttp.open("POST", 'saveLog.php', true);
  xhttp.send(data);
}

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 of console.log

var logFileString="";
var maxLogLength=1024*128;

console.save=function(){
  var logArgs={};

  for(var i=0; i<arguments.length; i++) logArgs['arg'+i]=arguments[i];
  console.log(logArgs);

  // keep a string representation of every log
  logFileString+=JSON.stringify(logArgs,null,2)+'\n';

  // save the string representation when it gets big
  if(logFileString.length>maxLogLength){
    // send a copy in case race conditions change it mid-save
    saveLog(logFileString);
    logFileString="";
  }
};

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:

function saveLog(data){
  // do some ajax stuff with data.
  var xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200) {}
  }

  xhttp.open("POST", 'saveLog.php', true);
  xhttp.send(data);
}

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

最佳男配角 2024-12-14 19:12:49

这个答案可能看起来特别相关,但是专门针对网络日志,您可以访问以下链接。

我发布此答案的原因是因为就我而言,console.log 打印了 长截断文本,因此我无法从控制台获取值。我通过直接从网络日志打印 api 响应来解决这个问题。

chrome://net-export/

您可能会看到与此类似的窗口,只需按开始记录到磁盘按钮即可:

在此处输入图像描述

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.

chrome://net-export/

There you may see a similar windows to this, just press the Start Logging to Disk button and that's it:

enter image description here

昔日梦未散 2024-12-14 19:12:49

一种方法是将结果转换为字符串,并将该字符串数据转换为 blob< /a> 然后到 数据 url 并制作可使用 标签和 click() 下载

// Convert string data to url
var results = { test1: 'result1', test2: 'result2' }
let blob = new Blob([JSON.stringify(results)], {type: 'text/plain'})
let url = URL.createObjectURL(blob)

// Create anchor tag
let downloadLink = document.createElement('a')
downloadLink.download = 'results.txt'
downloadLink.href = url

// Click to download file
downloadLink.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 and click()

// Convert string data to url
var results = { test1: 'result1', test2: 'result2' }
let blob = new Blob([JSON.stringify(results)], {type: 'text/plain'})
let url = URL.createObjectURL(blob)

// Create anchor tag
let downloadLink = document.createElement('a')
downloadLink.download = 'results.txt'
downloadLink.href = url

// Click to download file
downloadLink.click()
梦明 2024-12-14 19:12:49

使用以下命令创建批处理文件并将其另存为桌面上的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文