-
Notifications
You must be signed in to change notification settings - Fork 1
Logging Strategy For Node Modules
This document is intended to explain what to use and how to perform a successful logging approach when creating Node Modules.
Simple answer to a simple question, logging helps a lot when things go wrong and we need to investigate what exactly went wrong, It's possible that in order to see the problem we need to have vision inside of what happening on a library that we are using (in this case we are talking about a Node.js environment, so our libraries will be node modules).
Although our libraries could have a 100% test coverage (the utopia is real) It's always good to have a way to see what's happening inside of it, this is where logging shines.
There are multiple logging libraries out there for the Node.js ecosystem, however there is one that fits the most important case that we need specially because we are doing node modules logging, we need to be able to turn off (preferably to be turn off by default) the logging mechanism, we don't want to pollute the stdout of users that use our library with a bunch of information that might be irrelevant (most of the time) to them.
The perfect library for us is Debug, basically it provide us a namespace for logging, it doesn't log anything by default (we must explicitly pass a DEBUG option to the command with the namespace we want to activate).
What to logging is not an easy question, it's highly dependent in the nature of your library and what would you consider worth of logging, something to have in mind is that worth is also highly relative because this information will only appear if the user explicitly turn on the logging system.
Said this It's recommended to log most of the things, giving priority to things that transform or work with user's input (remember that we are talking about a node module here, so user's input here is another developer's input probably).
Talking about the how now, normally logged messages are sign by a timestamp, then again because this is a node module this might be necessary or not (depending on the needs that the library is covering), most of the time you won't need this.
Whatever our library will be logging must be under the correct namespace (in this case the name of the project is what best suite here) and if we need to show even deeper levels we just use something like namespace.firstLevel.secondLevel and that should works for most of the cases.
When printing data we must find a way to pretty print it, the Debug library does have a way to accomplish this we just need to check their documentation for this part.
const debug = require('debug')('my-module');
debug('Debugging in my-module level');
$ DEBUG=my-module node app.js
// > my-module Debugging in my-module level +0ms
const debug = require('debug')('my-multiple.level.log');
debug('Debugging in my multiple level log');
$ DEBUG=my-multiple.level.log node app.js
// > my-multiple.level.log Debugging in my multiple level log +0ms
Pretty simple and pretty straightforward.
References Node.JS logging tutorial