Best practice allowing users to enter registrar into nodejs modules

I wrote this module for nodejs, which can be used to send events from anywhere through sockjs for clients.

Now, I would like to enable some custom logging mechanisms.

Right now I am adding winston as a dependency, requiring it as a registrar in each class and using logger.error, logger.warn, ...

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

and

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

Now, how can I get the logger to be a replaceable user logger or to configure the user log level from OUTSIDE of my module?

They, of course, do not need to touch the module to change the registrar, but be able to create a registrar with loglevel, and my module should use it if it is available.

Any recommendations on how to do this?

Or is it a hard dependency on "winston" ok and configure the log level through npm-config?

+3
dependency-injection logging winston
Mar 11 '14 at 20:12
source share
2 answers

I suggest not including Winston at all. Winston is very popular and contains many nice features that your users may not use, and I believe that your module should be small enough so that the user always uses all its functions. If there is a case where they do not want to use the built-in recorder, well, I think it is not small enough. With that said, a module that records material when necessary for debugging is useful.

If you want the developer to use your module to debug your module using logs, I suggest you turn on debugging mode, which you can turn on / off.

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

And if the user wants to integrate his own logging system, I suggest that you can pass on the logging function to your module. Something like this (let me name your rock-the-sock module:

 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 will have a default registrar that can be overridden as shown above.

socket.io is a good example of how this is done (although you really don't need the configure method, which is just twists):

Here you can override the default registrar socket.io: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

Where as a registrar is implemented as follows: https://github.com/LearnBoost/Socket.IO/blob/0.9.14/lib/logger.js

And if something ever goes wrong, try throwing an error or throwing an error event or call the error callback (no matter what is applicable). Silently registering it is not a good idea. If you report this, then let the user code process it. Perhaps the error was expected (you have no idea what crazy ways your modules use!).

+3
Apr 04 '14 at 16:20
source share

Although winston is the number one registrar used, there is also bunyan, which is also widely used. Therefore, yes, you must leave the choice to the user and allow him to enter the registrar into your module. The Elasticsearch client module understands this perfectly. You can do it the same way .

+1
Apr 24 '14 at 21:35
source share



All Articles