在node.js中捕获console.log吗? [英] Catching console.log in node.js?

查看:191
本文介绍了在node.js中捕获console.log吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在node.js中捕获由console.log(...)引起的最终控制台输出,以防止在单元测试模块时记录终端?

Is there a way that I can catch eventual console output caused by console.log(...) within node.js to prevent cloggering the terminal whilst unit testing a module?

谢谢

推荐答案

module.js:

module.js:

module.exports = function() {
    console.log("foo");
}

程序:

console.log = function() {};
mod = require("./module");
mod();
// Look ma no output!

显然,您可以根据需要收集日志消息,以备以后使用:

Obviously you can collect the log messages for later if you wish:

var log = [];
console.log = function() {
    log.push([].slice.call(arguments));
};

这篇关于在node.js中捕获console.log吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆