pino
pino copied to clipboard
adding Pino to global
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
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/
I would even recommend against using the global cache (for mostly the same reason.
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.