Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log timestamps in Chrome?

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?

like image 827
UpTheCreek Avatar asked Aug 17 '12 14:08

UpTheCreek


People also ask

What is console timestamp?

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.

How do I export my Chrome console log?

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.

How do I view console log in browser?

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.


2 Answers

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.

Update for Chrome 68+

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:

Show timestamps how-to picture

like image 133
Krzysztof Wolny Avatar answered Sep 19 '22 21:09

Krzysztof Wolny


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:

Sample output

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.

like image 33
JSmyth Avatar answered Sep 19 '22 21:09

JSmyth