UpT*_*eek 206 javascript google-chrome
有没有快速的方法让Chrome在console.log写入中输出时间戳(比如Firefox).或者是预先new Date().getTime()唯一的选择?
Krz*_*lny 375
在Chrome中,有一个选项是控制台设置(开发人员工具 - >控制台 - >设置[右上角]),名为"显示时间戳",这正是我所需要的.
我刚发现它.没有其他脏的黑客需要破坏占位符并删除记录消息的代码中的位置.
JSm*_*yth 79
试试这个:
console.logCopy = console.log.bind(console);
console.log = function(data)
{
var currentDate = '[' + new Date().toUTCString() + '] ';
this.logCopy(currentDate, data);
};
Run Code Online (Sandbox Code Playgroud)
或者,如果你想要一个时间戳:
console.logCopy = console.log.bind(console);
console.log = function(data)
{
var timestamp = '[' + Date.now() + '] ';
this.logCopy(timestamp, data);
};
Run Code Online (Sandbox Code Playgroud)
要登录多件事情 ,并在一个不错的方式(如对象树表示):
console.logCopy = console.log.bind(console);
console.log = function()
{
if (arguments.length)
{
var timestamp = '[' + Date.now() + '] ';
this.logCopy(timestamp, arguments);
}
};
Run Code Online (Sandbox Code Playgroud)
使用格式字符串(JSFiddle)
console.logCopy = console.log.bind(console);
console.log = function()
{
// Timestamp to prepend
var timestamp = new Date().toJSON();
if (arguments.length)
{
// True array copy so we can call .splice()
var args = Array.prototype.slice.call(arguments, 0);
// If there is a format string then... it must
// be a string
if (typeof arguments[0] === "string")
{
// Prepend timestamp to the (possibly format) string
args[0] = "%o: " + arguments[0];
// Insert the timestamp where it has to be
args.splice(1, 0, timestamp);
// Log the whole array
this.logCopy.apply(this, args);
}
else
{
// "Normal" log
this.logCopy(timestamp, args);
}
}
};
Run Code Online (Sandbox Code Playgroud)
输出与:

PS:仅在Chrome中测试过.
PPS:Array.prototype.slice在这里并不完美,因为它会被记录为一组对象而不是一系列对象.
Ser*_*zN1 18
您可以使用开发工具分析器.
console.time('Timer name');
//do critical time stuff
console.timeEnd('Timer name');
Run Code Online (Sandbox Code Playgroud)
"计时器名称"必须相同.您可以使用具有不同名称的多个计时器实例.
tek*_*irl 12
我最初将其添加为评论,但由于至少一个人找不到该选项(或者由于某种原因,该版本在其特定版本中不可用),因此我想添加一个屏幕截图。
在Chrome 68.0.3440.106(现在已在72.0.3626.121中检查)上,我不得不
A. *_*sky 10
ES6解决方案:
const timestamp = () => `[${new Date().toUTCString()}]`
const log = (...args) => console.log(timestamp(), ...args)
Run Code Online (Sandbox Code Playgroud)
其中timestamp()返回实际格式化的时间戳并log添加时间戳并将所有自己的参数传播到console.log
我转换arguments为数组使用,Array.prototype.slice以便我可以concat使用另一个我要添加的数组,然后将其传递给console.log.apply(console, /*here*/);
var log = function () {
return console.log.apply(
console,
['['+new Date().toISOString().slice(11,-5)+']'].concat(
Array.prototype.slice.call(arguments)
)
);
};
log(['foo']); // [18:13:17] ["foo"]
Run Code Online (Sandbox Code Playgroud)
它似乎arguments也可以Array.prototype.unshift编辑,但我不知道如果修改它是一个好主意/将有其他副作用
var log = function () {
Array.prototype.unshift.call(
arguments,
'['+new Date().toISOString().slice(11,-5)+']'
);
return console.log.apply(console, arguments);
};
log(['foo']); // [18:13:39] ["foo"]
Run Code Online (Sandbox Code Playgroud)
如果您使用的是谷歌Chrome浏览器,则可以使用chrome console api:
这两个呼叫之间经过的时间显示在控制台中.
有关详细信息,请参阅文档链接:https://developers.google.com/chrome-developer-tools/docs/console
小智 5
也试试这个:
this.log = console.log.bind( console, '[' + new Date().toUTCString() + ']' );
Run Code Online (Sandbox Code Playgroud)
该函数将时间戳、文件名和行号与内置的 console.log.
| 归档时间: |
|
| 查看次数: |
124865 次 |
| 最近记录: |