Is there any quick way of getting Chrome to output timestamps in console.log
writes (like Firefox does). Or is prepending new Date().getTime()
the only option?
The console. timeStamp method adds a single marker to the browser's Performance tool (Firefox, Chrome). This lets you correlate a point in your code with the other events recorded in the timeline, such as layout and paint events.
Chrome console log Select the 'Console'' tab and make sure the option 'Preserve log' is checked. Reproduce the issue. You'll see data being collected in the console window. Right click on any log statement in the console window, and click Save As… to save the log file to your computer.
Look for Developer Tools or Simply Tools menu in all major browsers. If you are using Google Chrome the press Cntrl+shift+j to see console. In Firefox, press Ctrl+Shift+I and click on Console to view the console on Firefox.
In Chrome, there is the option in Console Settings (Either press F1 or select Developer Tools -> Console -> Settings [upper-right corner] ) named "Show timestamps" which is exactly what I needed.
I've just found it. No other dirty hacks needed that destroys placeholders and erases place in the code where the messages was logged from.
The "Show timestamps" setting has been moved to the Preferences pane of the "DevTools settings", found in the upper-right corner of the DevTools drawer:
Try this:
console.logCopy = console.log.bind(console); console.log = function(data) { var currentDate = '[' + new Date().toUTCString() + '] '; this.logCopy(currentDate, data); };
Or this, in case you want a timestamp:
console.logCopy = console.log.bind(console); console.log = function(data) { var timestamp = '[' + Date.now() + '] '; this.logCopy(timestamp, data); };
To log more than one thing and in a nice way (like object tree representation):
console.logCopy = console.log.bind(console); console.log = function() { if (arguments.length) { var timestamp = '[' + Date.now() + '] '; this.logCopy(timestamp, arguments); } };
With format string (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); } } };
Outputs with that:
P.S.: Tested in Chrome only.
P.P.S.: Array.prototype.slice
is not perfect here for it would be logged as an array of objects rather than a series those of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With