抑制/省略nodejs中的重复日志

时间:2015-01-13 09:00:04

标签: node.js logging

我正在研究温斯顿,摩根等人,但我找不到任何可以改变它的东西:

[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
[NOTIFICATION] - inserting an item in the collection
像这样:

[NOTIFICATION] - inserting an item in the collection x 12
例如,在Chrome开发工具控制台中就像

一样。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以限制日志记录调用,下划线具有throttling功能:

var writeLog = _.throttle(function(message) {
    log.info(message);
}, 3000); // Throttled to once every 3 seconds

setInterval(function() {
    writeLog(new Date().toTimeString());
}, 1000);

您可能希望每秒写一条日志消息,但节流会每3秒产生一条消息