Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding Pino to global #1258

Closed
Ali-Hussein-dev opened this issue Dec 8, 2021 · 3 comments
Closed

adding Pino to global #1258

Ali-Hussein-dev opened this issue Dec 8, 2021 · 3 comments
Labels

Comments

@Ali-Hussein-dev
Copy link

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
@jsumners
Copy link
Member

jsumners commented Dec 8, 2021

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/

@mcollina
Copy link
Member

mcollina commented Dec 9, 2021

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

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants