运行单元测试时禁用 winston 日志记录? [英] Disable winston logging when running unit tests?

查看:19
本文介绍了运行单元测试时禁用 winston 日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行节点模块的单元测试时,可以有选择地禁用 Winston 日志记录吗?

Can Winston logging be selectively disabled when executing unit tests of a node module?

理想情况下,我希望在应用程序运行时进行日志记录以提供信息和调试目的,但在运行测试时被抑制以免使演示单元测试结果混乱.

Ideally, I'd like to have logging for informational and debug purposes when the application is running, but be suppressed as to not clutter the presentation unit test results when I run my tests.

我对 winston 的使用是我的模块内部的,如下所示:

My use of winston is internal to my module, something like this:

// MyModule.js
var logger = require('winston');
module.exports = function() {
  // does some stuff
  // and logs some stuff like so:
  logger.log('an informational message');
}

// MyModuleTest.js
describe('MyModule', fucntion() {
  it('should do some stuff', function() {
     var myModuleUnderTest = require('MyModule');
     // some tests
  }
}   

推荐答案

Winston 传输具有您可以设置的 silent 属性,这可能比删除整个传输要好一些.

Winston transports have a silent property that you can set, which is probably a little nicer than removing the entire transport.

我为传输添加了一个名称,这样会更容易一些:

I add a name to the transports to make is a little easier like this:

var logger = new winston.Logger();

logger.add(winston.transports.Console, {
    name: 'console.info',
    colorize: true,
    showLevel: true,
    formatter: consoleFormatter,
})

然后在测试或设置中,我可以有选择地打开和关闭日志:

Then in the test or set-up I can selectively turn logging on and off with:

logger.transports['console.info'].silent = true  // turns off
logger.transports['console.info'].silent = false // logging back on

这篇关于运行单元测试时禁用 winston 日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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