A tiny JavaScript debugging utility that works in Node.js and browsers. Use environment variables to control logging, so there are no ridiculous console log statements in production.
This is based on debug. It's been rewritten to use contemporary JS.
In the browser, this should work well with vite. As is
convention, this will look for an env variable prefixed with VITE_
. So pass
env variables like VITE_DEBUG="foo"
.
Featuring:
package.json
to choose node JS or browser versionPlus, see the docs generated by typescript.
Works with vite
or other systems. This will look at import.meta.env
, or
an arbitrary object you can pass in.
import Debug from '@substrate-system/debug'
// look at `import.meta.env.VITE_DEBUG`
const debug = Debug('example')
// or call with your own env object
const debug = Debug('example', { DEBUG: 'example' })
If you create an instance without passing in a namespace
string, then this
will log iff anything other than false
is passed as an argument.
import Debug from '@substrate-system/debug'
// log b/c we are not calling with `false`
const debug = Debug()
debug('hello')
You can use any variable as debug status:
import Debug from '@substrate-system/debug'
// in Vite
const debug = Debug(!!(import.meta.env && import.meta.env.DEV))
// in an arbitrary server
const debug = Debug(window.EXAMPLE_DEBUG_MODE)
Use an env variable of *
to log everything.
Build the site with a NODE_ENV
variable to set import.meta.env.DEV
:
NODE_ENV=development vite build
Any value of NODE_ENV
, except production
, wil equate to
import.meta.env.DEV
being true.
NODE_ENV=staging vite build
npm i -D @substrate-system/debug
Use this with vite in the browser, or in node.
Run your script with an env variable, DEBUG
.
// in node JS
import createDebug from '@substrate-system/debug/node'
const debug = createDebug('fooo')
debug('testing')
Call this with an env var of DEBUG=fooo
DEBUG=fooo node ./test/fixture/node.js
If you are in dev mode (process.env.NODE_ENV === 'development'
), then this will log things in a random color if you don't initialize it with a namespace --
import createDebug from '@substrate-system/debug'
const debug = createDebug()
debug('hello')
Run the script like this:
NODE_ENV=development node ./my-script.js
Start a vite
server and log some things. This uses the example directory.
npm start
Run tests:
npm test