@substrate-system/debug v0.9.2
debug
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:
- Use exports field in
package.jsonto choose node JS or browser version - ESM only
Plus, see the docs generated by typescript.
Contents
config
namespace
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.
NODE_ENV + Vite
Build the site with a NODE_ENV variable to set import.meta.env.DEV:
NODE_ENV=development vite buildAny value of NODE_ENV, except production, wil equate to
import.meta.env.DEV being true.
NODE_ENV=staging vite buildinstall
npm i -D @substrate-system/debugUse this with vite in the browser, or in node.
Node JS
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.jsNODE_ENV
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.jsdevelop
browser
Start a vite server and log some things. This uses the example directory.
npm starttest
node
Run tests:
npm test8 months ago
8 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
8 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago