是否有任何快速的方法让Chrome在console.log写入中输出时间戳(像Firefox那样)。或者是prepending new Date().getTime()是唯一的选项?
当前回答
扩展了JSmyth中“带格式字符串”的解决方案
所有其他console.log变量(log,debug,info,warn,error) 包括时间戳字符串灵活性参数(例如:09:05:11.518 vs. 2018-06-13T09:05:11.518Z) 包括回退,以防浏览器中不存在控制台或其功能
.
var Utl = {
consoleFallback : function() {
if (console == undefined) {
console = {
log : function() {},
debug : function() {},
info : function() {},
warn : function() {},
error : function() {}
};
}
if (console.debug == undefined) { // IE workaround
console.debug = function() {
console.info( 'DEBUG: ', arguments );
}
}
},
/** based on timestamp logging: from: https://stackoverflow.com/a/13278323/1915920 */
consoleWithTimestamps : function( getDateFunc = function(){ return new Date().toJSON() } ) {
console.logCopy = console.log.bind(console)
console.log = function() {
var timestamp = getDateFunc()
if (arguments.length) {
var args = Array.prototype.slice.call(arguments, 0)
if (typeof arguments[0] === "string") {
args[0] = "%o: " + arguments[0]
args.splice(1, 0, timestamp)
this.logCopy.apply(this, args)
} else this.logCopy(timestamp, args)
}
}
console.debugCopy = console.debug.bind(console)
console.debug = function() {
var timestamp = getDateFunc()
if (arguments.length) {
var args = Array.prototype.slice.call(arguments, 0)
if (typeof arguments[0] === "string") {
args[0] = "%o: " + arguments[0]
args.splice(1, 0, timestamp)
this.debugCopy.apply(this, args)
} else this.debugCopy(timestamp, args)
}
}
console.infoCopy = console.info.bind(console)
console.info = function() {
var timestamp = getDateFunc()
if (arguments.length) {
var args = Array.prototype.slice.call(arguments, 0)
if (typeof arguments[0] === "string") {
args[0] = "%o: " + arguments[0]
args.splice(1, 0, timestamp)
this.infoCopy.apply(this, args)
} else this.infoCopy(timestamp, args)
}
}
console.warnCopy = console.warn.bind(console)
console.warn = function() {
var timestamp = getDateFunc()
if (arguments.length) {
var args = Array.prototype.slice.call(arguments, 0)
if (typeof arguments[0] === "string") {
args[0] = "%o: " + arguments[0]
args.splice(1, 0, timestamp)
this.warnCopy.apply(this, args)
} else this.warnCopy(timestamp, args)
}
}
console.errorCopy = console.error.bind(console)
console.error = function() {
var timestamp = getDateFunc()
if (arguments.length) {
var args = Array.prototype.slice.call(arguments, 0)
if (typeof arguments[0] === "string") {
args[0] = "%o: " + arguments[0]
args.splice(1, 0, timestamp)
this.errorCopy.apply(this, args)
} else this.errorCopy(timestamp, args)
}
}
}
} // Utl
Utl.consoleFallback()
//Utl.consoleWithTimestamps() // defaults to e.g. '2018-06-13T09:05:11.518Z'
Utl.consoleWithTimestamps( function(){ return new Date().toJSON().replace( /^.+T(.+)Z.*$/, '$1' ) } ) // e.g. '09:05:11.518'
其他回答
JSmyth对答案的改进:
console.logCopy = console.log.bind(console);
console.log = function()
{
if (arguments.length)
{
var timestamp = new Date().toJSON(); // The easiest way I found to get milliseconds in the timestamp
var args = arguments;
args[0] = timestamp + ' > ' + arguments[0];
this.logCopy.apply(this, args);
}
};
这样的:
显示以毫秒为单位的时间戳 假设一个格式字符串作为.log的第一个参数
您可以使用开发工具分析器。
console.time('Timer name');
//do critical time stuff
console.timeEnd('Timer name');
“定时器名称”必须相同。您可以使用多个具有不同名称的计时器实例。
Chrome 98的更新如下:
设置—>首选项—>控制台—>显示时间戳
Chrome 68:
“显示时间戳”移至设置
以前在“控制台设置”中的“显示时间戳”复选框已移动到“设置”。
也试试这个:
this.log = console.log.bind( console, '[' + new Date().toUTCString() + ']' );
该函数将时间戳、文件名和行号与内置console.log相同。
如果您使用谷歌Chrome浏览器,您可以使用Chrome控制台api:
控制台。时间:在代码中你想要开始计时器的地方调用它 控制台。timeEnd:调用它来停止计时器
这两个调用之间的运行时间显示在控制台中。
有关详细信息,请参阅doc链接:https://developers.google.com/chrome-developer-tools/docs/console
推荐文章
- 如何在禁用按钮上启用引导工具提示?
- Node.js全局变量
- 在前一个函数完成后调用另一个函数
- JavaScript中两个日期之间的月份差异
- 如何同时运行两个jQuery动画?
- 如何将FormData (HTML5对象)转换为JSON
- Object.hasOwnProperty()产生ESLint 'no-prototype-builtins'错误:如何修复?
- 如何卸载Service Worker?
- 如何将Node.js流的内容读入字符串变量?
- 两个感叹号?
- jQuery ' .is(":visible") '在Chrome中无效
- 如何在JavaScript中计算今天之前三个月的日期?
- 如何保存样式面板的CSS Chrome开发工具的变化?
- <script defer="defer">到底是如何工作的?
- 设置ajax超时