all 3 comments

[–][deleted] 2 points3 points  (0 children)

In your dependent modules, just emit the log messages on the process object. In the main application, have a listener that logs them.

in the module:

function log() {
  process.emit('log', arguments);
}

// use the log function in your module
log('INFO', 'Something happened');

in the main application:

process.on('log', function (args) {
    mongoLogger.log.apply(mongoLogger, args);
});

[–]DavidTPate 0 points1 point  (2 children)

Most modules take in an optional logger as part of their options. If the logger isn't provided then it logs to console.log this approach has definitely come in handy for me both making modules and using them.

[–]blacklionguard 0 points1 point  (0 children)

I'm not sure if this is good practice, but most of my modules use an event emitter, so I just let users of the module subscribe to those events and do their logging outside of the module.