Common logging for node, express application — best practice?

后端 未结 2 976
广开言路
广开言路 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");
    
    0 讨论(0)
  • 2021-01-04 05:40

    if you are using express with node.js then you can try this. By default, logging is disabled in Express. You have to do certain stuff to get logs working for your app. For access logs, we need to use the Logger middleware; for error logs we will use Forever.Hope it will help you.. Here is a good example How to Logging Access and Errors in node.js

    0 讨论(0)
提交回复
热议问题