Last active
December 11, 2015 21:39
-
-
Save egorvinogradov/4664058 to your computer and use it in GitHub Desktop.
Useful console.log patch for Chrome showing how much time have been passed since last time console.log was run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
console.log output example: | |
===================================================================================== | |
Date Total Diff Message | |
===================================================================================== | |
Wed Jan 30 2013 3:3:47:376 0ms 0ms start | |
Wed Jan 30 2013 3:3:47:382 6ms +6ms started hotels loading | |
Wed Jan 30 2013 3:3:47:502 126ms +120ms loaded: /optimizely/main.js (1) | |
Wed Jan 30 2013 3:3:47:723 347ms +221ms DOM content loaded | |
*/ | |
console.log = function (){ | |
function repeatChar(char, count){ | |
return new Array(count + 1).join(char); | |
}; | |
function humanizeTime(timestamp){ | |
var humanized = []; | |
var date = new Date(timestamp); | |
var data = { | |
h: date.getUTCHours(), | |
m: date.getUTCMinutes(), | |
s: date.getUTCSeconds(), | |
ms: date.getUTCMilliseconds() | |
}; | |
for ( var prop in data ) { | |
if ( data[prop] || !true ) { | |
humanized.push(data[prop] + prop); | |
} | |
} | |
if ( !humanized.length ) { | |
humanized.push('0ms'); | |
} | |
return humanized.join(' '); | |
}; | |
const dateColumnLength = 37; | |
const totalTimeColumnLength = 18; | |
const prevTimeColumnLength = 15; | |
var currentTime = new Date(); | |
var diffStart = console._startTime ? +currentTime - +console._startTime : 0; | |
var diffPrevious = console._previousTime ? +currentTime - +console._previousTime : 0; | |
var value = [ | |
currentTime.toDateString(), ' ', | |
currentTime.getHours(), ':', | |
currentTime.getMinutes(), ':', | |
currentTime.getSeconds(), ':', | |
currentTime.getMilliseconds() | |
].join(''); | |
value += repeatChar(' ', dateColumnLength - value.length); | |
value += humanizeTime(diffStart); | |
value += repeatChar(' ', dateColumnLength + totalTimeColumnLength - value.length); | |
value += ( diffPrevious ? '+' : ' ' ) + diffPrevious + 'ms'; | |
value += repeatChar(' ', dateColumnLength + totalTimeColumnLength + prevTimeColumnLength - value.length); | |
Array.prototype.unshift.call(arguments, value); | |
if ( !console._startTime ) { | |
console._startTime = currentTime; | |
} | |
console._previousTime = currentTime; | |
console.constructor.prototype.log.apply(console, arguments); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment