简体   繁体   中英

Disable logging in Node.JS

So I want to disable logging for some of the environments for my Node.JS application (Express); is the following the best way to disable logging in Node.JS applications?

if(process.env.NODE_ENV == "production")
{
    console.log = function(){}; 
}

Does adding this piece of code in my app.js affect all the middle-wares and routes as well (I want to disable logging for whole application, routes, middle-wares,...)?

Thanks

You might want to move away from console.log and use a logging library such as winston , but it depends on how big your project is and how much flexibility you want.

By the way console.log = function() {}; will work, it just depends on whether you want some logging to work (as probably is the case, you might want to see errors) or not.

I've tested the previous by creating the following setup:

file 1.js

console.log = function() {};
require('./2');

file 2.js

// you will not see anything
console.log(2);

If you will decide to use it you will then be able to set logging levels , which will help you to not show verbose logs in a production environment.

For example:

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') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM