I'm building a library. Is there a logger that can be configured by the user?
For those that know Java, I'm basically asking if slf4j exists in node.
For those that don't know Java, I'm building a library that needs to output logs to:
1. Give me enough information to troubleshoot issues when apps use it.
2. Give users enough information to troubleshoot when they use it.
I don't want to always print these logs. Logging should be configurable by the user. I don't want to write my own logging library to achieve this. Is there a node.js logging package that can be configured externally?
PS: I've looked at node.js logger top 10 lists, and I didn't see any that touted this as a feature.
21 Replies
You can accept a function to call when logging, by default it could be
console.log
for instance, and you can also accept places to log for warnings, errors, and info
You can accept these as options when initialising
This would also allow people to extend what happens when logging with custom logic such as
Or if for some reason they need to change the formatting
Additionally you could add methods to attach specific loggers and it manages adding all of the different types of loggers, but then also expose being able to add the methods directly for those who want to add a logger you haven’t made a method forHey, so at my work, I usually use pino.
It has different levels of logs that you can use.. you should be able to use it on a browser build as well..
https://github.com/pinojs/pino/blob/master/docs/browser.md
GitHub
pino/docs/browser.md at master · pinojs/pino
🌲 super fast, all natural json logger. Contribute to pinojs/pino development by creating an account on GitHub.
I think this is one of the 10 I looked into. I'm not seeing where the option is to configure it externally
Yeah, that would work. It's pretty much what I'm already doing in another app/library dependency. But it feels like writing boilerplate code and I don't like how I would have to write it over and over again unless I turn the logging into its own library; there are many other logging libraries out there already so I feel like all I'd be contributing is a feature
Hmm, let's see if I understand you correctly,
Basically you want your users to let your library know what kind of log they want to see right?
Or maybe even disable it?
Yeah, regardless of what logging package the library uses
Maybe that's just a stretch goal
It should be possible,
https://betterstack.com/community/guides/logging/how-to-install-setup-and-use-pino-to-log-node-js-applications/#log-levels-in-pino
Just pass in the level your users want to see. I am assuming you will have to take in that option during init.
Or outright disable pino.
A Complete Guide to Pino Logging in Node.js | Better Stack Community
This tutorial will guide you through creating a production-ready logging system for your Node.js application using Pino
Can you check if that helps?
Note that log level is a number, you can create your own levels without much issue. Btw, I am only talking about this because you don't want make your tiny loggin system. So can easily follow the advice given above.
@Nihan That's effectively what I do now
I think I'm approaching this at the wrong angle: is it rare for node libraries to log any information?
No, it's. I can't live without logs haha...
Even if I am shipping something that will not be used by another dev, I add logs or at least try to...
we are both talking about libraries not apps, right?
yes, we are talking about libraries..
I have used app as an example to explain that logs are fairly common imo.
okay so it's common for libraries to log information: are they always off by default when they are used as a dependency?
@Nihan (I forgot to reply)
Ok, sry If I couldn't help...mostly because I am failing to understand the problem..
You can ignore btw... no worries
*ignore me
I'm probably describing it poorly, sorry
nah it's fine, it was just a network lag
I replied few seconds ago
before your message
anyway, yeah most smaller libraries don't include logging that much unless it wants to through an error or warn about something
ah
and big libraries are rare?
hmm, I would say even big libraries don't always log details that much
take react or pixijs for example. it will warn you
but it won't log what is happening
unless you ask it to or regardless?
lemme double check something
yep be default it won't log things in most cases that I have seen.
unless it's to warn about something...
@DanKaplanSES
note that the story changes if you are using a backend framework
it will give you logs by default. In the fronend however, browser console logs are frowned upon actually. Like in a pr you will be asked to remove it.
Okay, cool I guess you have left the conversation..
I will jump off then 😦
Cya
@DanKaplanSES
Sorry I was in the middle of writing another post
I'm back now
Yeah that makes sense because users would see those logs and they may slow down the app
probably other reasons too
okay so when you change the default, is it usually a simple on/off, or can log levels be configured?
That's a semi-rhetorical question: I'm going to assume the latter.
With that assumption, does that mean every lib with configurable logging needs to be configured in its own way? And does that mean the user has to learn that way each time they want to configure logging in a new library?
It depends really.
I think you are free to allow any of those really....also when you say learning,
usually logging will be kinda similar no matter package you use, it probably takes around 5 minutes to just skim the doc and see if there is anything new...