Common logging for node, express application — best practice?

后端 未结 2 1048
广开言路
广开言路 2021-01-04 04:46

I\'m working on an node.js application with several dozen modules and using bunyan for logging (JSON output, multiple configurable streams). I\'ve been looking for good e

2条回答
  •  遥遥无期
    2021-01-04 05:38

    Singleton pattern in nodejs - is it needed? Actually, singleton is perhaps not needed in Node's environment. All you need to do is to create a logger in a separate file say, logger.js:

    var bunyan = require("bunyan"); // Bunyan dependency
    var logger = bunyan.createLogger({name: "myLogger"});
    
    module.exports = logger;
    

    Then, retrieve this logger from another module:

    var logger = require("./logger");
    logger.info("Anything you like");
    

提交回复
热议问题