https://github.com/substrate-system/debug
Debug utility
https://github.com/substrate-system/debug
debug logger
Last synced: 12 months ago
JSON representation
Debug utility
- Host: GitHub
- URL: https://github.com/substrate-system/debug
- Owner: substrate-system
- License: other
- Created: 2023-11-01T06:49:32.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-07-04T05:51:21.000Z (12 months ago)
- Last Synced: 2025-07-04T06:32:27.329Z (12 months ago)
- Topics: debug, logger
- Language: TypeScript
- Homepage: https://substrate-system.github.io/debug/
- Size: 270 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# debug

[](README.md)
[](README.md)
[](https://semver.org/)
[](https://packagephobia.com/result?p=@substrate-system/debug)
[](LICENSE)
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](https://github.com/debug-js/debug). It's been rewritten to use contemporary JS.
In the browser, this should work well with [vite](https://vite.dev/). As is
convention, this will look for an env variable prefixed with `VITE_`. So pass
env variables like `VITE_DEBUG="foo"`.
**Featuring:**
* Use [exports](https://github.com/substrate-system/debug/blob/main/package.json#L31) field in `package.json` to choose node JS or browser version
* ESM only
Plus, [see the docs](https://substrate-system.github.io/debug/) generated by typescript.
## Contents
- [config](#config)
* [namespace](#namespace)
* [NODE_ENV + Vite](#node_env--vite)
- [install](#install)
- [Node JS](#node-js)
* [NODE_ENV](#node_env)
- [develop](#develop)
* [browser](#browser)
- [test](#test)
* [node](#node)
## config
### namespace
Works with `vite` or other systems. This will look at `import.meta.env`, or
an arbitrary object you can pass in.
```js
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.
```js
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:
```js
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`:
```sh
NODE_ENV=development vite build
```
Any value of `NODE_ENV`, except `production`, wil equate to
`import.meta.env.DEV` being true.
```sh
NODE_ENV=staging vite build
```
----------------------------------------------------------------------
## install
```sh
npm i -D @substrate-system/debug
```
Use this with [vite](https://vitejs.dev/) in the [browser](#browser), or
in [node](#node-JS).
------------------------------------------------------------------
## Node JS
Run your script with an env variable, `DEBUG`.
```js
// 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`
```sh
DEBUG=fooo node ./test/fixture/node.js
```
### NODE_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 --
```js
import createDebug from '@substrate-system/debug'
const debug = createDebug()
debug('hello')
```
Run the script like this:
```sh
NODE_ENV=development node ./my-script.js
```
-------------------------------------------------------------------
## develop
### browser
Start a `vite` server and log some things. This uses [the example directory](./example/).
```sh
npm start
```
## test
### node
Run tests:
```sh
npm test
```