簡體   English   中英

在Winston中動態更改日志級別

[英]Dynamically change the log level in winston

如何動態更改Winston中的日志級別,以反映多個文件中的級別更改?

我有兩個js文件,並且兩個文件中都包含記錄器。 如果我在index.js中更改記錄器的級別,則該更改不會反映在readfile.js中

這是我的代碼:

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)

不要將此問題標記為重復問題。 這些答案對我沒有幫助。

在readfile.js中,導入winston_logger的方式與在index.js中導入它的方式相同

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;

我可以從任何地方更改日志級別。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM