pinojs/pino

How to use a single logger instance across a project with multiple lib files

JohnPolansky opened this issue · 2 comments

So first I'm coming from a winston background for whatever that is worth, most of my projects are relative small in scale but typically involve a main.js and then a variety of ./lib/abc.js files to break up the code by purpose. What I'm trying to do is create a logger instance of pino that I can use/configure via main.js and then use in the rest of the ./lib/abc.js files. Specifically in winston I did this via logger.js with a export logger that created a basic logger then via main.js when I read the command line arguments I use winston.configure() to update the existing logger with various changes loglevel, file vs stdout, json vs pretty With logger.js i then import logger.js into the main and simple import winston into all lib files they all get and share the same logger this way.

This previous comment #1108 would seem to indicate that Pino was never designed to have a default logger on purpose, and I've seen a few posts where people reference the pino logger as immutable. This has honestly left me a bit stumped. What are my options to support a single shared logger that can be configured at run time?

  • Create/Configure the logger in main.js might fulfill part of the requirement, but it's only good in the main.js unless I then pass the logger instance into every single ./lib function I can't use it?
  • Accept the limitation that a logger cannot be configured post creation, and then use the ENV Variables to configure the logger dynamically. and use the export method to share it across libs.
    • Issue here is that if I do this and then use log.level = debug to raise the log level and then in one of my libs use log.child({module: abc}) it appears to create the child based on the settings of the original instance from logger.js and I loose the loglevel change
  • Create a new logger in every ./lib and pass in logger options from main.js so I'd have to call a setupLogger(options) for every lib file requiring logs. This feels like it would be bad practice and maybe waste for system resources/performance.

Honestly I feel like I must be missing something obvious because this feels like something common a lot of folks would use. I really appreciate any help I've been beating my head against the wall for awhile.

You seem to rely heavily on globals & singletons. Don't.

Here is a longform explanation: https://blog.platformatic.dev/unlocking-the-power-of-nodejs.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.