是否有任何快速的方法让Chrome在console.log写入中输出时间戳(像Firefox那样)。或者是prepending new Date().getTime()是唯一的选项?
当前回答
Chrome 98的更新如下:
设置—>首选项—>控制台—>显示时间戳
Chrome 68:
“显示时间戳”移至设置
以前在“控制台设置”中的“显示时间戳”复选框已移动到“设置”。
其他回答
Chrome 98的更新如下:
设置—>首选项—>控制台—>显示时间戳
Chrome 68:
“显示时间戳”移至设置
以前在“控制台设置”中的“显示时间戳”复选框已移动到“设置”。
我使用Array.prototype.slice将参数转换为Array,这样我就可以连接到我想添加的另一个Array,然后将它传递到console.log。应用(控制台,/ * * /);
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"]
似乎参数也可以是array .prototype. unmoved,但我不知道这样修改是否是个好主意/会有其他副作用
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"]
试试这个:
console.logCopy = console.log.bind(console);
console.log = function(data)
{
var currentDate = '[' + new Date().toUTCString() + '] ';
this.logCopy(currentDate, data);
};
或者这样,如果你想要一个时间戳:
console.logCopy = console.log.bind(console);
console.log = function(data)
{
var timestamp = '[' + Date.now() + '] ';
this.logCopy(timestamp, data);
};
以一种很好的方式(如对象树表示)记录多个事物:
console.logCopy = console.log.bind(console);
console.log = function()
{
if (arguments.length)
{
var timestamp = '[' + Date.now() + '] ';
this.logCopy(timestamp, arguments);
}
};
带格式字符串(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);
}
}
};
输出:
附注:仅在Chrome中测试。
array .prototype.slice在这里并不完美,因为它将被记录为对象的数组,而不是对象的一系列。
+new Date和Date.now()是获取时间戳的替代方法
如果希望保留行号信息(每个消息都指向它的.log()调用,而不是所有消息都指向包装器),则必须使用.bind()。您可以通过console.log预先添加一个额外的时间戳参数。Bind (console, <timestamp>),但问题是你每次都需要重新运行这个函数来获得一个新的时间戳绑定。 一种尴尬的方法是返回一个绑定函数:
function logf() {
// console.log is native function, has no .bind in some browsers.
// TODO: fallback to wrapping if .bind doesn't exist...
return Function.prototype.bind.call(console.log, console, yourTimeFormat());
}
然后必须与双重调用一起使用:
logf()(object, "message...")
但是,我们可以通过安装带有getter函数的属性来隐式地进行第一次调用:
var origLog = console.log;
// TODO: fallbacks if no `defineProperty`...
Object.defineProperty(console, "log", {
get: function () {
return Function.prototype.bind.call(origLog, console, yourTimeFormat());
}
});
现在您只需调用console.log(…),它就会自动添加一个时间戳!
> console.log(12)
71.919s 12 VM232:2
undefined
> console.log(12)
72.866s 12 VM233:2
undefined
你甚至可以通过Object.defineProperty(window, "log",…)来实现简单的log()而不是console.log()。
请参阅https://github.com/pimterry/loglevel,了解使用.bind()的安全控制台包装器,并提供兼容性回退。
参见https://github.com/eligrey/Xccessors获取从defineProperty()到遗留__defineGetter__ API的兼容性回调。 如果这两个属性API都不起作用,则应该退回到每次都获得一个新的时间戳的包装器函数。(在这种情况下,你会丢失行号信息,但时间戳仍然会显示。)
Boilerplate:我喜欢的时间格式:
var timestampMs = ((window.performance && window.performance.now) ?
function() { return window.performance.now(); } :
function() { return new Date().getTime(); });
function formatDuration(ms) { return (ms / 1000).toFixed(3) + "s"; }
var t0 = timestampMs();
function yourTimeFormat() { return formatDuration(timestampMs() - t0); }
推荐文章
- 如何在单个测试基础上更改模拟实现?
- VueJS有条件地为元素添加属性
- Uncaught TypeError:(中间值)(…)不是一个函数
- 如何设置URL查询参数在Vue与Vue路由器
- 了解Chrome网络日志“停滞”状态
- 无法读取属性“addEventListener”为空
- 如何防止moment.js从webpack加载地区?
- getMonth在javascript中给出前一个月
- 如何在禁用按钮上启用引导工具提示?
- Node.js全局变量
- 在前一个函数完成后调用另一个函数
- JavaScript中两个日期之间的月份差异
- 如何同时运行两个jQuery动画?
- 如何将FormData (HTML5对象)转换为JSON
- Object.hasOwnProperty()产生ESLint 'no-prototype-builtins'错误:如何修复?