1

I have a web application in which I have used many console.logs that are printing required information on console. I want the timestamp of each console.log to identify which console.log called exactly when. Is there is any general method by which we can do it?

For this should I go manually in and add the time stamp in each console.log or there is any method by which we can do it?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Abhishek
  • 31
  • 1
  • 4

3 Answers3

1

On Chrome, there's a setting that allows you to do it.

If you want to do it via JavaScript, there's a nice implementation:

console.log2 = console.log.bind(console);

console.log = function(...msg) {
    const stamp = `[${Date.now()} ]`;
    this.log2(stamp, ...msg);
};
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

How about using Date?

let i = 0;

setInterval(() => console.log(Date(), `=> message ${++i}`), 1000);

or write a wrapper as this answer suggests. I'd prefer not to redefine console.log in favor of:

const log = (...msgs) => console.log(Date(), ...msgs);

log("hey!");
ggorlen
  • 44,755
  • 7
  • 76
  • 106
0

The simplest way is appending new Date() to console.log like,

console.log("You console message. Timestamp = ",new Date());
ravibagul91
  • 20,072
  • 5
  • 36
  • 59