using winston in several modules

2019-01-30 10:18发布

I have several modules - let's say server.js, module1.js,...,moduleN.js.

I would like define the log file in my server.js:

winston.add(winston.transports.File, { filename: 'mylogfile.log' });

and then use it in all my modules.

What is the best way to do that? I could exports.winston=winston; in each module and then set it in the server.js, but is there any better solution?

Thank you in advance!

7条回答
劳资没心,怎么记你
2楼-- · 2019-01-30 11:15

I wanted to use custom colours and levels.

So I removed the default console-transport and set a colorized one

here is my logger.js

var logger = require('winston');

logger.setLevels({
    debug:0,
    info: 1,
    silly:2,
    warn: 3,
    error:4,
});
logger.addColors({
    debug: 'green',
    info:  'cyan',
    silly: 'magenta',
    warn:  'yellow',
    error: 'red'
});

logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { level: 'debug', colorize:true });

module.exports = logger;



Loading from app.js:

var logger = require('./lib/log.js');  



Loading from other modules:

 var logger = require('winston');        
查看更多
登录 后发表回答