debug icon indicating copy to clipboard operation
debug copied to clipboard

Possibility to read in config at debug instance creation instead of at first (version) require/ import?

Open webketje opened this issue 2 years ago • 3 comments

I love debug's simplicity, but it bugs me that it is so tightly coupled to environment variables (NodeJS impl) I would like to integrate it in Metalsmith along with an abstraction of env.

Basically, as soon as a package where debug is used, is require'd, or perhaps with some NPM-derivatives, if multiple packages use the same version, debug reads its defaults from the environment.

const pkgContainingDebug = require('pkg-with-debug') // debug config set according to DEBUG_COLORS= (not defined)
process.env.DEBUG_COLORS = false
const localDebug = require('debug') // debug config takes into account dynamically set process.env.DEBUG_COLORS
const localDebugger= localDebug('app') // nothing happens here config-wise
localDebugger('Hello world') 
pkgContainingDebug() // different config from localDebugger

This makes it impossible (in my case for metalsmith) to provide a user with a programmatic way of setting debug values, unless the current package + all its dependencies use the same debug dependency (other typical use case: expressjs) If the env reading was done at debugger creation instead of require, this would provide a window to decouple the tight coupling of debug configuration and env vars across debug dependencies

const pkgContainingDebug = require('pkg-with-debug') // debug config not initialized unless dependency instantiates it at inclusion
const localDebug = require('debug') // does nothing yet
process.env.DEBUG_COLORS = false
const localDebugger= localDebug('app') // reads in env
localDebugger('Hello world') 
pkgContainingDebug() // same config as localDebugger if the dependency's debugger only happens when the dep is first run

Hope this makes sense. I haven't had the time to dive into the codebase for this yet

webketje avatar Jun 03 '22 00:06 webketje

Yeah makes sense, it's definitely a drawback. The next major version was planned to change a lot of this with full backwards compatibility but with the shifting loader system in Node a lot of the plans have been tabled until the ESM loaders reach parity.

I don't really have a great answer for you right now, unfortunately :/

Qix- avatar Jun 03 '22 01:06 Qix-

4.3.4

RabiaSaid avatar Jun 21 '22 11:06 RabiaSaid

2.6.8

RabiaSaid avatar Jun 21 '22 12:06 RabiaSaid