3

In the Node.js application, I use Winston for logging. Right now, the winston.level is set from process.env.LOG_LEVEL variable.

How to reset the winston.level whenever there is a change in process.env.LOG_LEVEL without restarting the Node process or server.

Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51
  • how is the change to `process.env.LOG_LEVEL` made in your server? – shanks Dec 18 '18 at 07:22
  • it can be any possible way, 1. Modifying it in .bash_profile 2. Using export command 3. Through docker image or Kubernetes dashboard – Kamalakannan J Dec 19 '18 at 05:42
  • 1
    yes but you'd have to restart the process for the changes to take effect. I dont see any other way. – shanks Dec 19 '18 at 09:46
  • If not environment variables, is there any approach to solve my problem ? – Kamalakannan J Dec 20 '18 at 05:26
  • I'd totally advance against doing that in fact. You will only introduce a security hole in your application if you can change environment variables the way you want. Configuration should be read-only and immutable. If it needs to change then it's good practice to do that during build time on your CI/CD server. – shanks Dec 22 '18 at 09:23

2 Answers2

3

You can use debug npm module to do the trick. This code will help you to enable or disable debug logs without restarting your node application.

You can use debug module along with winston, where winston will log normal application logs, and for debugging you have to use this module.

/**
 *  To test this code 
 *  1. npm install debug express and npm start = to run a server
 *  2. goto http://localhost:8181/ = (simmulate running batch process) to start the logging on every second
 *  3. goto http://localhost:8181/change/myapp:db = to eable debug log
 *  4. goto http://localhost:8181/disable = to disable debug log
 * 
 *  Note: Don't foget to monitor your console, after each request. :P
 */
const debug = require('debug');
const express = require('express');

const intiLog = debug('myapp:init');
const batchProcessLog = debug('myapp:batch');
const dbLog = debug('myapp:db');

const app = express();

app.get('/', (req, res) => {
    setInterval(() => {
        console.log('This normal console.log');
        intiLog('This is init log');
        batchProcessLog('Hey this is batch process');
        dbLog('This is DB logs');
    }, 1000);
    return res.status(200).send('OK');
});

// nameSpance = myapp:init => enable only one log
// nameSpace = myapp:db,myapp:myappbatch => enable multiple log
// nameSpace = myapp:*,-myapp:init => enable all log except myapp:init log
app.get('/change/:nameSpace', (req, res) => {
    const { nameSpace } = req.params;
    debug.enable(nameSpace);
    return res.status(200).send(`May the force be with you ${nameSpace}`);
});

app.get('/disable', (req, res) => {
    debug.disable();    
    return res.status(200).send(`See you than...`);
});


app.listen(8181, () => console.log(`Running app on ${8181}`));

Note: This code might not be ready for production use

For security reason you should put this API behind Authentication and Authorization check.

Vinay Pandya
  • 3,020
  • 2
  • 26
  • 42
0

@KamalakannanJ you might try to use RnR library to change winston log level on runtime without restarting the node server.

Here is the link: https://www.npmjs.com/package/runtime-node-refresh

Przemek Nowicki
  • 572
  • 4
  • 7