Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically change the log level in winston #1125

Closed
devarajchidambaram opened this issue Nov 2, 2017 · 2 comments
Closed

Dynamically change the log level in winston #1125

devarajchidambaram opened this issue Nov 2, 2017 · 2 comments

Comments

@devarajchidambaram
Copy link

devarajchidambaram commented Nov 2, 2017

how to dynamically change the log level in winston which will reflect a level change in multiple files.

I have two js files, in both files, I'm including logger, Let say If I change the level of the logger in index.js, but it will not reflect in readfile.js

Here is my code

winston_logger.js

var winston = require('winston');
var logger;
module.exports.init = function (logLevel) {
      logger = new(winston.Logger)({
        transports: [
            new(winston.transports.Console)({
                level: logLevel
            })
        ],
        exitOnError: false
    });
}

module.exports.getLogger = function (config) {
    if (!module.exports.logger) {
        exports.init('info')
    }
    return logger;
}

module.exports.debugLevel = function(){
  logger.transports.console.level = 'debug'
}

Index.js

var  readFile = require('./readFile')
var winston = require('./winston_logger')
var log = winston.getLogger()
log.info("info message" );

/*Here Im changing the log level as debug*/
winston.debugLevel ();

setTimeout(function(){
    readFile(function(){})
}, 5000)

readfile.js

var  readFile = require('./readFile')
var winston = require('./winston_logger')
var log = winston.getLogger()
log.info("info message" );

/*Here Im changing the log level as debug*/
winston.debugLevel ();

setTimeout(function(){
    readFile(function(){})
}, 5000)

Don't add these as a duplicate issue. these answers are not helped me

@devarajchidambaram
Copy link
Author

devarajchidambaram commented Nov 2, 2017

winston_logger.js

   var winston = require('winston');
    winston.configure({
        transports: [
          new (winston.transports.Console)({ timestamp: true, json : false , colorize: true}),
          new (winston.transports.File)({filename: './somefile.log' , json : true, timestamp: true })
        ]
     });

module.exports = winston;

I can able to change the log level from anywhere. Thanks.

with this basic configuration, I can run this code in production server as well?

@indexzero
Copy link
Member

Closing as a duplicate of #1191. This is on our roadmap and will be fixed before winston@3.0.0 gets released on May 29th, 2018.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants