0.9.2 • Published 15 days ago

@substrate-system/debug v0.9.2

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
15 days ago

debug

tests module types semantic versioning install size 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. 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.json to 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 build

Any value of NODE_ENV, except production, wil equate to import.meta.env.DEV being true.

NODE_ENV=staging vite build

install

npm i -D @substrate-system/debug

Use 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.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 --

import createDebug from '@substrate-system/debug'
const debug = createDebug()
debug('hello')

Run the script like this:

NODE_ENV=development node ./my-script.js

develop

browser

Start a vite server and log some things. This uses the example directory.

npm start

test

node

Run tests:

npm test
0.7.22

3 months ago

0.7.21

3 months ago

0.7.24

2 months ago

0.9.0

2 months ago

0.8.0

2 months ago

0.9.2

15 days ago

0.9.1

25 days ago

0.7.20

3 months ago

0.7.18

5 months ago

0.7.17

5 months ago

0.7.16

5 months ago

0.7.15

5 months ago

0.7.14

6 months ago

0.7.12

6 months ago

0.7.11

6 months ago

0.7.10

6 months ago

0.7.9

6 months ago

0.7.8

6 months ago

0.7.7

6 months ago

0.7.6

6 months ago

0.7.5

6 months ago

0.7.4

7 months ago

0.7.2

7 months ago