Created
February 15, 2014 21:09
-
-
Save nmcv/9025283 to your computer and use it in GitHub Desktop.
Add timestamp or tamper with console.log any way you want, including format string specifiers ("%s", "%d" etc.) support
This file contains hidden or 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.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); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment