I am building a Node.JS project (P) and a library (L). P depends on L. L is complex enough to warrant different categories of logging.
I've never used a JavaScript logging library before, but I'm experienced with Java loggers (e.g., log4j). Java loggers are built to be configured externally, that is, I can set L's log levels from P.
But I've looked into three Node.JS logging libraries so far, and even though I like winston's API enough to give it a try, none of them provided documentation on configuring the logger externally. All the examples require changing L's source code to change its log levels.
So here's my question: how do I configure L's winston log levels from P's code/configuration?
If I had to come up with my own solution, I probably use See my answer below.process.env.level.<containerName> in each L module like this:
But given the rest of the winston API, this seems really low level and it feels like there should be an easier way. More importantly, it's unlikely other libraries would come up with the same implementation, so their logging would not be externally configurable.
Since I needed a solution, I came up with this typescript implementation:
It's used like this:
const logger = getLogger("<your category>");I won't mark this as accepted because this is only useful if a library uses similar code. I'm looking for a solution that exists for all libraries that use winston, not just mine.