An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# debug
![tests](https://github.com/substrate-system/debug/actions/workflows/nodejs.yml/badge.svg)
[![module](https://img.shields.io/badge/module-ESM-blue?style=flat-square)](README.md)
[![types](https://img.shields.io/npm/types/@substrate-system/debug?style=flat-square)](README.md)
[![semantic versioning](https://img.shields.io/badge/semver-2.0.0-blue?logo=semver&style=flat-square)](https://semver.org/)
[![install size](https://flat.badgen.net/packagephobia/install/@substrate-system/debug)](https://packagephobia.com/result?p=@substrate-system/debug)
[![license](https://img.shields.io/badge/license-Polyform_Non_Commercial-26bc71?style=flat-square)](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
```