pino icon indicating copy to clipboard operation
pino copied to clipboard

adding Pino to global

Open Ali-Hussein-dev opened this issue 3 years ago • 2 comments

Is there is an issue if I call pino and add it to the global so that I do not have to import it in every file and call it? I am a frontend dev!

import pino from "pino"
const logger= pino({})
global.logger= logger

Ali-Hussein-dev avatar Dec 08 '21 12:12 Ali-Hussein-dev

This is not a recommended practice. Sharing global state means it is indeterministic what the status of that state is. Basically, you must make sure the global state is built the same way always and is never mutated. At a minimum this makes testing difficult. More drastically, it can make the application behavior unpredictable and the code base difficult to maintain. See https://softwareengineering.stackexchange.com/a/148109

We highly recommend:

lib/logger.js:

const pino = require('pino')
const logger = pino({ /* whatever options */ })
module.exports = logger

any_other_file_in_the_project.js:

const log = require('./lib/logger')
log.info('logger loaded')

This makes use of the require cache -- https://nodejs.org/en/knowledge/getting-started/what-is-require/

jsumners avatar Dec 08 '21 13:12 jsumners

I would even recommend against using the global cache (for mostly the same reason.

mcollina avatar Dec 09 '21 08:12 mcollina

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.

github-actions[bot] avatar Feb 18 '23 00:02 github-actions[bot]