You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are two env variables to work with, `VITE_DEBUG` and `VITE_DEBUG_MODE` in the browser, and `DEBUG` and `NODE_ENV` in node.
49
-
50
-
To determine if we should log something, first we check `NODE_ENV` / `VITE_DEBUG_MODE`. By default, in the browser we check `import.meta.env.DEV`, and in node, `NODE_ENV` should be either `development` or `test`. But -- [you can configure this](#configure).
51
-
52
-
Next we check the namespace + the env variable `VITE_DEBUG` in the browser, and `DEBUG` in node.
53
-
54
-
If the `DEBUG`/`VITE_DEBUG` variable is not set and you are in dev mode, then we log. If the `DEBUG`/`VITE_DEBUG` variable is set, then we only log if it matches the namespace that was passed in to debug, i.e.
41
+
### namespace
42
+
If you pass in a variable of the form `VITE_DEBUG=example`, then this will log
43
+
any instances created with the `example` namespace:
55
44
56
45
```js
57
46
importDebugfrom'@substrate-system/debug'
58
-
constdebug=Debug('hello-123')
59
-
60
-
debug('hello world')
47
+
constdebug=Debug('example')
61
48
```
62
49
63
-
The character `*` is a wildcard. In the preceding example, `'hello world` would be logged if we started vite with either `VITE_DEBUG=hello-*` or `VITE_DEBUG=hello-123`.
50
+
If you create an instance without passing in a `namespace` string, then this
51
+
will log iff the website is in `DEV` mode (if `import.meta.env.DEV` is true).
Build the site with a `NODE_ENV` variable to set `import.meta.env.DEV`:
67
56
68
-
## configure
69
-
70
-
Set the property `shouldLog` on `Debug` to configure which modes we log in. In the browser, this function gets called with `import.meta.env.MODE`. In node, it is called with `process.env.NODE_ENV`.
This is ergonomic with the [vite](https://vitejs.dev/) bundler. This module will look for an env variable prefixed with `VITE_`:
83
57
```sh
84
-
VITE_DEBUG=fooo
58
+
NODE_ENV=development vite build
85
59
```
86
60
87
-
Given then above env variable in vite, you would log like this:
88
-
```js
89
-
importDebugfrom'@substrate-system/debug'
90
-
constdebug=Debug('fooo')
91
-
debug('hello fooo')
92
-
```
93
-
94
-
#### *
95
-
Use an environment variable of `*` to log everything.
61
+
This will work too. Any value of `NODE_ENV`, except `production`, wil equate to
62
+
`import.meta.env.DEV` being true.
96
63
97
64
```sh
98
-
VITE_DEBUG="*"
65
+
NODE_ENV=staging vite build
99
66
```
100
67
101
-
#### DEV mode
102
-
103
-
If you initialize this without a namespace, then it checks `import.meta.env.DEV`:
104
-
```js
105
-
importDebugfrom'@substrate-system/debug'
106
-
constdebug=Debug()
107
-
debug('debug works') // check if `import.meta.env.DEV`
108
-
```
109
-
110
-
#### For example, in the staging environment:
111
-
112
-
```sh
113
-
VITE_DEBUG_MODE=staging vite build --mode staging
114
-
```
115
-
116
-
#### use multiple modes
117
-
Can parse a comma separated list of modes.
118
-
119
-
A `.env` file like this:
120
-
```sh
121
-
VITE_DEBUG_MODE="test, staging"
122
-
```
123
-
124
-
Will log in either "test" or "staging" modes, or if `import.meta.env.DEV` is true.
68
+
## install
125
69
126
70
```sh
127
-
vite --mode staging build
128
-
```
129
-
130
-
**If you are in production** (`import.meta.env.PROD`) and there is no `VITE_DEBUG` env var, then this exports a noop, so debug will do nothing, and your bundle will be smaller.
You would start that script with a `VITE_DEBUG=fooo` env var to see the log statements.
141
-
142
-
#### Don't use a namespace
143
-
If you call this without a `namespace` argument, it will look at the value of `import.meta.env.DEV`. If you are in DEV mode, then it will log things in a random color:
@@ -172,7 +91,7 @@ Call this with an env var of `DEBUG=fooo`
172
91
DEBUG=fooo node ./test/fixture/node.js
173
92
```
174
93
175
-
#### NODE_ENV
94
+
### NODE_ENV
176
95
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 --
177
96
178
97
```js
@@ -186,21 +105,6 @@ Run the script like this:
186
105
NODE_ENV=development node ./my-script.js
187
106
```
188
107
189
-
##### Configure the environment value
190
-
Configure what `NODE_ENV` value will trigger logging by overriding the `shouldLog` function:
0 commit comments