[英]Disable logging in Node.JS
所以我想为我的 Node.JS 应用程序 (Express) 禁用某些环境的日志记录; 以下是在 Node.JS 应用程序中禁用日志记录的最佳方法吗?
if(process.env.NODE_ENV == "production")
{
console.log = function(){};
}
在我的 app.js 中添加这段代码是否也会影响所有中间件和路由(我想禁用整个应用程序、路由、中间件等的日志记录...)?
谢谢
您可能希望远离console.log
并使用诸如winston 之类的日志记录库,但这取决于您的项目有多大以及您想要多大的灵活性。
顺便说一下console.log = function() {};
会起作用,这仅取决于您是否希望某些日志记录工作(可能是这种情况,您可能希望看到错误)。
我已经通过创建以下设置测试了前一个:
文件 1.js
console.log = function() {};
require('./2');
文件 2.js
// you will not see anything
console.log(2);
如果您决定使用它,您将能够设置日志级别,这将帮助您不在生产环境中显示详细日志。
例如:
var winston = require('winston');
winston.level = process.env.NODE_ENV == 'production' ? 'error' : 'debug';
// will show 'ERROR' in production logs
winston.error('ERROR')
// will show 'called test function' in an environment
// different from production
winston.debug('called test function')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.