javascript 是否可以将日期/时间绑定到控制台日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18410119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to bind a date/time to a console log?
提问by Naftali aka Neal
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:
当我想将前面带有的内容记录[DEBUG]
到控制台时,哪个有效。现在我想在日志中添加一个日期/时间,我试过这个:
var myLog = console.log.bind(console, '[DEBUG ' + (new Date) + ']');
Which obviously does not work because it alwayslogs the same time (the time that the .bind
was called).
这显然不起作用,因为它总是记录相同的时间(.bind
被调用的时间)。
Is there any way (using .bind
) to log the current time on each log withouthaving to do this:
有没有办法(使用.bind
)在每个日志上记录当前时间而不必这样做:
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 notthe line that myLog
was called.
因为上面的方法向我显示了console.log.apply
被调用的行而不是被调用的行myLog
。
回答by Shmiddty
Yes. http://jsfiddle.net/SwFJg/6/
是的。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.
这是有效的,因为每次都toString
被调用timestamp
(实际上,所有东西)都console.log
被调用。
We overwrite the default toString
method, and replace it with a time stamp (obviously you can change the output to whatever you want).
我们覆盖默认toString
方法,并用时间戳替换它(显然您可以将输出更改为您想要的任何内容)。
I chose the above pattern because, as others have noted (in SO chat), you can easily extend the DEBUG object to do other things.
我选择上述模式是因为,正如其他人所指出的(在 SO 聊天中),您可以轻松扩展 DEBUG 对象来做其他事情。
...
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
}
...
回答by Hari Krishna Tumpudi
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);
};