How to use Winston in several modules?

前端 未结 11 1563
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 23:40

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.trans         


        
11条回答
  •  情歌与酒
    2020-12-23 00:19

    What I do ( which may not be the best way ) is use a 'global' module where I export all the stuff that I use through my applications. For instance:

    //Define your winston instance
    winston.add(winston.transports.File, { filename: 'mylogfile.log' });
    exports.logger = winston;
    
    exports.otherGlobals = ....
    

    Now just require this globally used module from your other modules

    var Global = require(/path/to/global.js);
    

    Because the file is cached after the first time it is loaded (which you can verify by including a log statement in your global; it will only log once), there's very little overhead in including it again. Putting it all into one file is also easier than requiring ALL your globally used modules on every page.

提交回复
热议问题