11# debug
2- ![ tests] ( https://github. com/substrate-system/debug/actions/workflows/nodejs.yml/badge.svg )
2+ [ ![ tests] ( https://img.shields.io/ github/actions/workflow/status/substrate-system/debug/nodejs.yml?style=flat-square )] ( https://github. com/substrate-system/debug/actions/workflows/nodejs.yml)
33[ ![ module] ( https://img.shields.io/badge/module-ESM-blue?style=flat-square )] ( README.md )
44[ ![ types] ( https://img.shields.io/npm/types/@substrate-system/debug?style=flat-square )] ( README.md )
55[ ![ semantic versioning] ( https://img.shields.io/badge/semver-2.0.0-blue?logo=semver&style=flat-square )] ( https://semver.org/ )
88
99
1010A tiny JavaScript debugging utility that works in Node.js and browsers.
11- Use environment variables to control logging, so there are no ridiculous
12- console log statements in production.
11+ Use environment variables to control logging in Node.js, and localStorage
12+ to control logging in browsers, so there are no ridiculous console log
13+ statements in production.
1314
1415This is based on [ debug] ( https://github.com/debug-js/debug ) .
1516It's been rewritten to use contemporary JS.
1617
17- In the browser, this should work well with [ vite] ( https://vite.dev/ ) . As is
18- convention, this will look for an env variable prefixed with ` VITE_ ` . So pass
19- env variables like ` VITE_DEBUG="foo" ` .
18+ In the browser, this uses localStorage to control debug output.
19+ In Node.js, it uses the environmen variable ` DEBUG ` .
2020
2121** Featuring:**
2222* Use [ exports] ( https://github.com/substrate-system/debug/blob/main/package.json#L31 )
@@ -30,154 +30,116 @@ generated by typescript.
3030
3131<!-- toc -->
3232
33+ - [ Install] ( #install )
3334- [ Browser] ( #browser )
34- - [ config] ( #config )
35- * [ namespace] ( #namespace )
36- * [ NODE_ENV + Vite] ( #node_env--vite )
37- - [ install] ( #install )
3835- [ Node JS] ( #node-js )
39- * [ NODE_ENV] ( #node_env )
40- - [ develop] ( #develop )
36+ - [ Config] ( #config )
37+ * [ Namespace] ( #namespace )
38+ * [ Enable all namespaces] ( #enable-all-namespaces )
39+ * [ Enable specific namespaces] ( #enable-specific-namespaces )
40+ * [ Force logging with boolean true] ( #force-logging-with-boolean-true )
41+ - [ Develop] ( #develop )
4142 * [ browser] ( #browser )
42- - [ test ] ( #test )
43- * [ node ] ( #node )
43+ - [ Test ] ( #test )
44+ * [ Node ] ( #node )
4445
4546<!-- tocstop -->
4647
47- ## Browser
48-
49- In the browser, this will look for ` VITE_DEBUG ` import.meta variable,
50- and ` DEBUG ` in ` localStorage ` .
51-
52- ``` js
53- import Debug from ' @substrate-system/debug'
54-
55- localStorage .setItem (' DEBUG' , ' browser:123' )
48+ ## Install
5649
57- const debug = Debug (' browser:*' )
58-
59- debug (' hello logs' )
60- // will log, b/c DEBUG in localStorage is browser:123
50+ ``` sh
51+ npm i -D @substrate-system/debug
6152```
6253
54+ ** Browser usage** : Use ` localStorage ` ` DEBUG ` key.
55+ ** Node.js usage** : Use environment variable ` DEBUG ` .
6356
64- ## config
65-
66- ### namespace
67- Works with ` vite ` or other systems. This will look at ` import.meta.env ` , or
68- an arbitrary object you can pass in, or ` localStorage ` in the browser.
69-
70- ``` js
71- import Debug from ' @substrate-system/debug'
72-
73- // look at `import.meta.env.VITE_DEBUG`
74- const debug = Debug (' example' )
75-
76- // or call with your own env object
77- const debug = Debug (' example' , { DEBUG : ' example' })
78- ```
57+ ## Browser
7958
80- If you create an instance without passing in a ` namespace ` string, then this
81- will log iff anything other than ` false ` is passed as an argument.
59+ In the browser, this looks for the ` DEBUG ` key in ` localStorage ` .
8260
8361``` js
8462import Debug from ' @substrate-system/debug'
8563
86- // log b/c we are not calling with `false`
87- const debug = Debug ( )
64+ // Set DEBUG in localStorage
65+ localStorage . setItem ( ' DEBUG ' , ' myapp:* ' )
8866
89- debug (' hello' )
67+ const debug = Debug (' myapp:component' )
68+ debug (' hello logs' )
69+ // will log, because DEBUG in localStorage matches 'myapp:*'
9070```
9171
92- You can use any variable as debug status:
72+ ## Node JS
73+ Run your script with an env variable, ` DEBUG ` .
9374
9475``` js
95- import Debug from ' @substrate-system/debug'
96-
97- // in Vite
98- const debug = Debug (!! (import .meta.env && import.meta.env.DEV))
99-
100- // in an arbitrary server
101- const debug = Debug (window .EXAMPLE_DEBUG_MODE )
76+ // in node JS
77+ import createDebug from ' @substrate-system/debug/node'
78+ const debug = createDebug (' fooo' )
79+ debug (' testing' )
10280```
10381
104- Use an env variable of ` * ` to log everything.
105-
106-
107- ### NODE_ENV + Vite
108-
109- Build the site with a ` NODE_ENV ` variable to set ` import .meta.env.DEV` :
110-
82+ Call this with an env var of ` DEBUG=fooo `
11183``` sh
112- NODE_ENV = development vite build
84+ DEBUG=fooo node ./test/fixture/node.js
11385```
11486
115- Any value of ` NODE_ENV ` , except ` production` , wil equate to
116- ` import .meta.env.DEV` being true.
87+ ## Config
11788
118- ` ` ` sh
119- NODE_ENV = staging vite build
120- ` ` `
89+ ### Namespace
90+ In browsers, this checks ` localStorage ` for ` DEBUG ` key. In Node.js, this checks
91+ the environment variable ` DEBUG ` .
12192
122- ----------------------------------------------------------------------
93+ ``` js
94+ import Debug from ' @substrate-system/debug'
12395
124- ## install
96+ // In browser: checks localStorage.getItem('DEBUG')
97+ const debug = Debug (' example' )
12598
126- ` ` ` sh
127- npm i - D @substrate - system / debug
99+ // Log if no namespace is set in localStorage
100+ const debug = Debug ( import .meta.env.DEV)
128101` ` `
129102
130- Use this with [vite](https://vitejs.dev/) in the [browser](#browser), or
131- in [node](#node-JS).
132-
133-
134- ------------------------------------------------------------------
135-
136-
137- ## Node JS
138- Run your script with an env variable, ` DEBUG ` .
103+ ### Enable all namespaces
139104
140105` ` ` js
141- // in node JS
142- import createDebug from ' @substrate-system/debug/node'
143- const debug = createDebug (' fooo' )
144- debug (' testing' )
106+ localStorage .setItem (' DEBUG' , ' *' )
145107` ` `
146108
147- Call this with an env var of ` DEBUG = fooo `
148- ` ` ` sh
149- DEBUG = fooo node . / test / fixture / node . js
109+ ### Enable specific namespaces
110+ ` ` ` js
111+ localStorage . setItem ( ' DEBUG' , ' myapp:auth,myapp:api ' )
150112` ` `
151113
152- ### NODE_ENV
153- 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 --
114+ ### Force logging with boolean true
115+
116+ You can also pass ` true ` to force logging regardless of ` localStorage` .
117+ For example, in Vite, this will log on your localhost server, but not
118+ in production:
154119
155120` ` ` js
156- import createDebug from ' @substrate-system/debug'
157- const debug = createDebug ()
158- debug (' hello' )
121+ const debug = Debug (import .meta.env.DEV)
122+ debug (' This logs' )
159123` ` `
160124
161- Run the script like this:
162- ` ` ` sh
163- NODE_ENV = development node ./ my- script .js
164- ` ` `
125+ ------------------------------------------------------------------
165126
166- -------------------------------------------------------------------
167127
168- ## develop
128+ ## Develop
169129
170130### browser
171- Start a ` vite` server and log some things. This uses [the example directory](./example/).
131+ Start a ` vite` server and log some things. This uses
132+ [the example directory](./example/).
172133
173134` ` ` sh
174135npm start
175136` ` `
176137
177138
178- ## test
139+ ## Test
140+
141+ ### Node
179142
180- ### node
181143Run tests:
182144
183145` ` ` sh
0 commit comments