是否有任何快速的方法让Chrome在console.log写入中输出时间戳(像Firefox那样)。或者是prepending new Date().getTime()是唯一的选项?
当前回答
我使用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"]
其他回答
+new Date和Date.now()是获取时间戳的替代方法
如果您使用谷歌Chrome浏览器,您可以使用Chrome控制台api:
控制台。时间:在代码中你想要开始计时器的地方调用它 控制台。timeEnd:调用它来停止计时器
这两个调用之间的运行时间显示在控制台中。
有关详细信息,请参阅doc链接:https://developers.google.com/chrome-developer-tools/docs/console
ES6解决方案:
const timestamp = () => `[${new Date().toUTCString()}]`
const log = (...args) => console.log(timestamp(), ...args)
其中timestamp()返回实际格式化的时间戳和日志添加一个时间戳,并将所有自己的参数传播到console.log
您可以使用开发工具分析器。
console.time('Timer name');
//do critical time stuff
console.timeEnd('Timer name');
“定时器名称”必须相同。您可以使用多个具有不同名称的计时器实例。
如果希望保留行号信息(每个消息都指向它的.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); }
推荐文章
- 转换php数组到Javascript
- 如何跳转到浏览器页面的顶部
- 在电子邮件消息中是否支持JavaScript ?
- 如何在JavaScript中指定Math.log()的基?
- 多重左赋值JavaScript
- 如何在单个测试基础上更改模拟实现?
- VueJS有条件地为元素添加属性
- Uncaught TypeError:(中间值)(…)不是一个函数
- 如何设置URL查询参数在Vue与Vue路由器
- 了解Chrome网络日志“停滞”状态
- 无法读取属性“addEventListener”为空
- 如何防止moment.js从webpack加载地区?
- getMonth在javascript中给出前一个月
- 如何在禁用按钮上启用引导工具提示?
- Node.js全局变量