让用户将记录器注入Node.js模块的最佳实践 [英] Best practice to let users inject a logger into nodejs modules

查看:70
本文介绍了让用户将记录器注入Node.js模块的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为nodejs编写了此模块,该模块可用于通过sockjs从任何地方向客户端分发事件.

I wrote this module for nodejs that can be used to dispatch events from anywhere via sockjs to the clients.

现在,我想包括一些可配置的日志记录机制.

Now I'd like to include some configurable logging mechanisms.

此刻,我正在添加winston作为依赖项,要求它作为每个类中的记录器,并使用logger.error,logger.warn,...

At the moment, I'm adding winston as a dependency, require it as a logger in each class and use logger.error, logger.warn, ...

#logger.js
var logger = require('winston');
module.exports=logger;

#myClass
var logger = require("./logger");
logger.error("Something went wrong")

现在,如何使记录器可由自定义记录器替换,或者让用户从模块外部配置日志级别?

Now, how can I make the logger be replaceable by a custom logger or have the user configure the log level from OUTSIDE my module?

他们当然不必触摸模块代码来更改记录器,但能够创建具有日志级别的记录器,并且我的模块应该使用它(如果有).

They should of course not have to touch the module code to change the logger but be able to create the logger with loglevel and my module should use it, if available.

关于如何做到这一点的任何建议?

Any receommendations on how to do that?

还是对"winston"有严格的依赖关系,并且可以通过npm-config配置日志级别吗?

Or is a hard dependency on "winston" ok and have the log level be configurable via npm-config?

推荐答案

我建议完全不包括Winston. Winston很大,并且包含许多用户可能不会使用的出色功能,并且我相信您的模块应该足够小,以使用户将始终使用其所有功能.如果有些情况他们不想使用内置记录器,那么我猜它还不够小.话虽如此,在调试时需要的模块日志记录会很有帮助.

Well I suggest to not include Winston at all. Winston is kinda huge and contains a lot of nice features that your users might not use, and I believe that your module should be small enough that the user will always use all it's features. If there's a case that they don't wanna use the built-in logger, well it's not small enough I guess. With that said, a module logging stuff when needed for debugging is helpful.

如果您想让开发人员使用模块来调试带有日志的模块,那么我建议您使用一种可以打开/关闭的调试模式.

If you wanna have way for the developer using your module to debug your module with logs, then I suggest you to have a debug mode that can be turned on/off.

您可以使用类似的方法来帮助您: https://www.npmjs.org/package/调试

You could use something like this to help you: https://www.npmjs.org/package/debug

如果用户想集成他们自己的日志记录系统,我建议您采用一种方法,使他们可以向您的模块传递日志记录功能.这样的事情(我们称您的模块为摇滚:

And if the user wants to integrate their own logging system, I suggest you to have a way that they could pass your module a logging function. Something like this(let's call your module rock-the-sock:

var rockTheSock = require('rock-the-sock');
var rockTheSock.logger = function (level, message, metadata) {
    console.log('level :', level, message, metadata); // TODO: Integrate winston
};
rockTheSock.startRocking();

然后在您的模块中,您将有一个默认的记录器,可以如上所述将其覆盖.

And then in your module you would have a default logger, which can overriden as shown above.

socket.io是一个很好的示例(尽管您并不需要真正的configure方法,但这只是花哨的事情):

socket.io is a nice example of how it's done(though you don't really need the configure method, that's just bells and whistles):

这是您覆盖socket.io默认记录器的方式: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

Here's how you would override socket.io default logger: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

以这种方式实现记录器的地方: https ://github.com/LearnBoost/Socket.IO/blob/0.9.14/lib/logger.js

Where as the logger is implemented this way: https://github.com/LearnBoost/Socket.IO/blob/0.9.14/lib/logger.js

如果发生任何错误,请尝试引发错误,或发出错误"事件,或使用错误调用回调(执行任何适用的操作).静默记录它不是一个好主意.如果要报告它,则让用户代码处理它.也许是预期的错误(您不知道人们使用模块的疯狂方式!).

And if ever something goes wrong, try to throw the error, or emit an 'error' event or call the callback with the error(do whichever applicable). Silently logging it, is not a good idea. If you are reporting it, then let the user code handle it. Maybe the error was expected(you have no idea in what crazy ways people use your modules!).

这篇关于让用户将记录器注入Node.js模块的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆