简体   繁体   English

节点的常见日志记录,快速应用 - 最佳实践?

[英]Common logging for node, express application — best practice?

I'm working on an node.js application with several dozen modules and using bunyan for logging (JSON output, multiple configurable streams). 我正在开发一个带有几十个模块的node.js应用程序,并使用bunyan进行日志记录(JSON输出,多个可配置流)。 I've been looking for good examples of how to implement a instance across all the modules, but haven't seen what appears to be a really clean example I can learn from. 我一直在寻找如何在所有模块中实现实例的很好的例子,但是还没有看到我可以从中学到的一个非常干净的例子。

Below illustrates an approach that works, but seems quite inelegant (ugly) to me. 下面说明了一种有效的方法,但对我来说似乎相当不优雅(丑陋)。 I'm new to node & commonjs javascript in general, so looking for recommendations on how to improve it. 我一般都是node&commonjs javascript的新手,所以寻找有关如何改进它的建议。

module: ./lib/logger module:./ lib / logger

// load config file (would like this to be passed in to the constructor)
nconf.file({ file: fileConfig});
var logSetting = nconf.get('log');

// instantiate the logger
var Bunyan = require('bunyan');
var log = new Bunyan({
    name: logSetting.name,
streams : [
        { stream  : process.stdout, 
        level : logSetting.stdoutLevel},
        {    path : logSetting.logfile, 
            level : logSetting.logfileLevel}
    ],
serializers : Bunyan.stdSerializers
});

function Logger() {
};

Logger.prototype.info = function info(e) { log.info(e) };
Logger.prototype.debug = function debug(e) { log.debug(e) };
Logger.prototype.trace = function trace(e) { log.trace(e) };
Logger.prototype.error = function error(e) { log.error(e) };
Logger.prototype.warn = function warn(e) {  log.warn(e) };

module.exports = Logger;

module: main app 模块:主应用程序

// create the logger
var logger = require('./lib/logger)
var log = new logger();

// note: would like to pass in options -->  new logger(options)


module: any project module using logger
// open the logger (new, rely on singleton...)
var logger = require('./lib/logger');
var log = new logger();

or view the gist 查看要点

any recommendations? 任何建议?

EDIT: 编辑:

I've modified the constructor, making the singleton pattern explicit (rather than implicit as part of the 'require' behaviour. 我修改了构造函数,使单例模式显式化(而不是隐式作为'require'行为的一部分。

var log = null;
function Logger(option) {

// make the singleton pattern explicit
if (!Logger.log) {
    Logger.log = this;
}
    return Logger.log;
};  

and then changed the initialization to take an options parameter 然后更改初始化以获取选项参数

// initialize the logger 
Logger.prototype.init = function init(options) {
log = new Bunyan({
    name: options.name,
    streams : [
        { stream  : process.stdout, 
            level : options.stdoutLevel},
        {    path : options.logfile, 
            level : options.logfileLevel}
    ],
    serializers : Bunyan.stdSerializers     
    });
};

Singleton pattern in nodejs - is it needed? nodejs中的单例模式 - 是否需要? Actually, singleton is perhaps not needed in Node's environment. 实际上,在Node的环境中可能不需要单例。 All you need to do is to create a logger in a separate file say, logger.js: 您需要做的就是在一个单独的文件中创建一个记录器,比如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");

if you are using express with node.js then you can try this. 如果你使用express与node.js,那么你可以试试这个。 By default, logging is disabled in Express. 默认情况下,Express中禁用日志记录。 You have to do certain stuff to get logs working for your app. 您必须执行某些操作才能使日志适用于您的应用。 For access logs, we need to use the Logger middleware; 对于访问日志,我们需要使用Logger中间件; 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 对于错误日志,我们将使用Forever.Hope它会帮助你..这是一个很好的例子如何记录 node.js中的访问和错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node Express编写API函数样式/设计模式的最佳实践 - Node Express writing API functions styles/design patterns best practice node.js&express - 应用程序结构的全局模块和最佳实践 - node.js & express - global modules & best practices for application structure 文件结构的最佳做法-使用请求模块(Node.js + Express.js) - Best practice on file structure - using Request module (Node.js + Express.js) Node.JS / Express视图中的页眉/页脚/可重用模板的最佳实践? - Best Practice for Headers/Footer/Reusable Templates in Node.JS/Express Views? (Node.js/Express) 将数据从控制器发送到 pug 视图然后由 Javascript 处理时的最佳实践 - (Node.js/Express) Best practice when sending data from controller to pug view which is then processed by Javascript node.js和express-使用多个中间件与回调-最佳实践是什么? - node.js and express - using multiple middlewares vs. callbacks - what's the best practice? 使用具有自己/共同依赖关系的模块的最佳实践 - Best practice for using modules with their own/common dependencies 分开通用代码的最佳设计实践? - best design practice to separate common code? 处理页面的常见“结构”元素的最佳实践? - best practice for dealing with common “structural” elements of pages? 节点的最佳实践-Mongo-Angle - Best practice for node - mongo - angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM