-
Notifications
You must be signed in to change notification settings - Fork 948
Description
Hi, I hoped to ask a question that would help clarify some confusion I'm seeing in other related issues about the debug logs from the instances not being enabled thus logs not appearing when users expect it should be enabled.
It seems this is often an order of operations issue. People expect the environment variables to be evaluated when an instance is created, but based on the responses to other issues, it seems the variables are evaluated when the module is imported which is causing the confusion. Example #750
The reason I had come across this is because I was trying to get get dotenv
to work with this package debug
. I think these are two very common solution and could work together more easily.
Common expectation (DOES NOT LOG)
import debug from 'debug'
import dotenv from 'dotenv'
dotenv.config()
console.log(`DEBUG=${process.env.DEBUG}`)
const debugInfo = debug('info')
debugInfo('Expect this to be logged')
Output:
node .
DEBUG=*
You can see it does work if the environment variables is set BEFORE the process is started in the output below
$env:DEBUG='*'; node .
DEBUG=*
info Expect this to be logged +0ms
From my understanding the reason this doesn't work is because the environment state is evaluated when the module is imported. And in the example the environment variables are only set after dotenv.config()
is called.
This means all instances created by the debug will be disabled event if DEBUG=*
is set. This is very confusing.
Order of operations
- debug imported (DEBUG is not set)
- dotenv.config() called, DEBUG is set
- debug instance created (but thinks DEBUG is unset so instance is disabled)
Questions:
- Is this explanation correct?
- Is it possible to re-evaluate the environment when the instance is created rather than only once on import?
- If so, how do you achieve this? and can it be set as a default behavior?
I saw there is some mention of ability to do this manually as seen here const debugInstance = debug('someInstance', {reloadEnv: true})
from this comment #750 (comment)
However, I was not able to get it to work and it's also not in the typescript definitions.
I don't know what the reason would be for the single evaluation on import, but I was thinking this would actually be a good default option as it aligns with expectation and only those users very concerned about performance would opt if there is some significant penalty.
I create a repo with reproduction here if there is a way to fix it:
https://github.com/mattmazzola/debugtest