Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bind a date/time to a console log?

I have the following code:

var myLog = console.log.bind(console, '[DEBUG]');

Which works find when I want to log things prepended with [DEBUG] to the console. Now I want to add a date/time to the log and I tried this:

var myLog = console.log.bind(console, '[DEBUG ' + (new Date) + ']');

Which obviously does not work because it always logs the same time (the time that the .bind was called).

Is there any way (using .bind) to log the current time on each log without having to do this:

var myLog = function(){
    var args = ['[DEBUG ' + (new Date) + ']'];
    for(var i = 0; i < arguments.length; ++i) {
        args.push(arguments[i]);
    }
    return console.log.apply(console, args);
};

?

Because the above method shows me the line that console.log.apply was called and not the line that myLog was called.

like image 509
Naftali Avatar asked Aug 23 '13 19:08

Naftali


People also ask

Does console log take time?

Will it affect the speed of script execution in production environments? Of course, console. log() will reduce your program's performance since it takes computational time.

How do I show time in console?

Press F1 from inside dev tools. Under the Console heading, you'll find Show timestamps .

What does console log () do?

The console. log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.


2 Answers

Yes. http://jsfiddle.net/SwFJg/6/

var DEBUG = (function(){
    var timestamp = function(){};
    timestamp.toString = function(){
        return "[DEBUG " + (new Date).toLocaleTimeString() + "]";    
    };

    return {
        log: console.log.bind(console, '%s', timestamp)
    }
})();

DEBUG.log("banana", {foo:'bar'}); //[DEBUG 2:43:21 PM] banana Object {foo: "bar"}
console.log("Peppercorn");        //Peppercorn 
DEBUG.log("apple");               //[DEBUG 2:43:21 PM] apple 
DEBUG.log("orange");              //[DEBUG 2:43:21 PM] orange 
setTimeout(function(){
    DEBUG.log("mango");           //[DEBUG 2:43:25 PM] mango 
},3000)

This works because toString is called on timestamp (and, in fact, everything) each time console.log is called.

We overwrite the default toString method, and replace it with a time stamp (obviously you can change the output to whatever you want).

I chose the above pattern because, as others have noted (in SO chat), you can easily extend the DEBUG object to do other things.

...
return {
    log: console.log.bind(console, '%s', timestamp),
    error: console.error.bind(console, '%s', timestamp),
    info: console.info.bind(console, '%s', timestamp),
    warn: console.warn.bind(console, '%s', timestamp),
    group: ...,
    groupEnd: ...,
    groupCollapsed: ... // etc
}
...
like image 101
Shmiddty Avatar answered Oct 22 '22 14:10

Shmiddty


I think this is what you are looking for, which is simple

console.logCopy = console.debug.bind(console);

console.debug = function(data)
{
    var currentDate = '[' + new Date().toUTCString() + '] ';
    this.logCopy(currentDate, data);
};
like image 22
Hari Krishna Tumpudi Avatar answered Oct 22 '22 14:10

Hari Krishna Tumpudi