Last Updated: February 25, 2016
· mahnunchik

NodeJS and Loggers - Modules (part 2)

Now, if you are developing a module you have several ways to connect the logger to your module. Kind of module does not matter at all (public or private).

Method 1. Logger as a configuration parameter of the module

You describe in the readme something like: logger that you can pass to the module should respond to methods for example info and error

 * @module My awesome module
 * @ param {Object} config - Config of my awesome module.
 * @ param {Logger} config.logger - Logger that will be used by module.

module.exports = function (config){

And then you have to add more requirements to the logger, for example how many arguments logger can get. And then you have to add more requirements...

But still something breaks at the end user.

Method 2. Using full logging library

For example, you can use the full logging library in your module like winston or bunyan. But first it is overhead. And second, the users can not simply change the format of the messages in the log. They almost can not do anything with logger in your module. And if they can then they get the dependencies hell...

Method 3. Using magic of mag

You can use the mag in your module. mag - it's a simple replacement of console. You get a well-formatted output while you are developing.

If users of your module thought about formatting of log messages they can require mag-hub in their application and get access to the stream of all log objects. They can make any transformation with your messages before writing them to stdout.

More examples here: mag-examples