use*_*729 2 javascript node.js console.log
所以我想为我的 Node.JS 应用程序 (Express) 禁用某些环境的日志记录;以下是在 Node.JS 应用程序中禁用日志记录的最佳方法吗?
if(process.env.NODE_ENV == "production")
{
console.log = function(){};
}
Run Code Online (Sandbox Code Playgroud)
在我的 app.js 中添加这段代码是否也会影响所有中间件和路由(我想禁用整个应用程序、路由、中间件等的日志记录...)?
谢谢
您可能希望远离console.log并使用诸如winston 之类的日志库,但这取决于您的项目有多大以及您想要多大的灵活性。
顺便说一下console.log = function() {};,这仅取决于您是否希望某些日志记录工作(可能是这种情况,您可能希望看到错误)。
我已经通过创建以下设置测试了前一个:
文件 1.js
console.log = function() {};
require('./2');
Run Code Online (Sandbox Code Playgroud)
文件 2.js
// you will not see anything
console.log(2);
Run Code Online (Sandbox Code Playgroud)
如果您决定使用它,您将能够设置日志级别,这将帮助您不在生产环境中显示详细日志。
例如:
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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7206 次 |
| 最近记录: |