In my Node.js applications, I often find it helpful to have an application log so that should something go wrong in production, I can have more information than I get from a crash stack trace to track down problems.
To facilitate this, I use a global instance of Winston which is set up as one of the first things in my code:
var winston = require('winston'); global.applog = new (winston.Logger)({ transports: [ new (winston.transports.Console)({ timestamp: true, level: 'debug' }) ] }); I then call applog.debug(), applog.info(), applog.warn(), and applog.error() as appropriate throughout my code. Even if I am in a class included from the main application, I will often send debug messages.
For my purposes, this works great. I get the information I need to keep things running smoothly, and handle unexpected behaviors. However:
- This reduces the reuse of any lib code as it cannot stand on its own without having a reference to
applog. - It doesn't feel like a best practice to have global references used inside modular object-oriented code.
- I don't think I would ever use a log utility that didn't have the standard methods (
debug,info,warn,error), but if I did switch away from Winston it's possible that all my code would need to be updated.
Is there a more appropriate pattern I should be following for logging?