Skip to content

Commit b8239e5

Browse files
authored
docs: add 'How to use' section to README [ci skip] (#2)
1 parent 55fe075 commit b8239e5

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,148 @@ sub('This is a sub-namespace debug message')
7272
console.log(sub.namespace) // 'my-namespace:sub-namespace'
7373
```
7474

75+
## How to use
76+
77+
### Enabling debug output
78+
79+
obug picks up enabled namespaces from the `DEBUG` environment variable in Node.js, or from `localStorage.debug` in browsers.
80+
81+
In Node.js:
82+
83+
```bash
84+
DEBUG=app:* node app.js
85+
```
86+
87+
On Windows (CMD):
88+
89+
```cmd
90+
set DEBUG=app:* & node app.js
91+
```
92+
93+
On Windows (PowerShell):
94+
95+
```powershell
96+
$env:DEBUG='app:*'; node app.js
97+
```
98+
99+
In the browser, set the value in DevTools and refresh the page:
100+
101+
```js
102+
localStorage.debug = 'app:*'
103+
```
104+
105+
### Wildcards and exclusion
106+
107+
The `*` character is a wildcard. Suppose your library has debuggers named `connect:bodyParser`, `connect:compress`, and `connect:session` — instead of listing each one, use `DEBUG=connect:*`. Use `DEBUG=*` to enable everything.
108+
109+
Exclude namespaces by prefixing them with `-`:
110+
111+
```bash
112+
DEBUG=*,-connect:* node app.js
113+
```
114+
115+
### Namespace conventions
116+
117+
If you're using obug in a library, prefix your namespaces with the library name and use `:` to separate features (e.g. `connect:bodyParser`). This lets users opt into the parts they care about without guessing names.
118+
119+
### Extending a namespace
120+
121+
Use `.extend()` to create a sub-namespace that inherits options from its parent:
122+
123+
```ts
124+
const log = createDebug('auth')
125+
const logSign = log.extend('sign') // 'auth:sign'
126+
const logLogin = log.extend('login') // 'auth:login'
127+
128+
log('hello') // auth hello
129+
logSign('hello') // auth:sign hello
130+
logLogin('hello') // auth:login hello
131+
```
132+
133+
### Formatters
134+
135+
obug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting. Built-in formatters:
136+
137+
| Formatter | Representation |
138+
| --------- | -------------------------------------------------- |
139+
| `%O` | Pretty-print an Object on multiple lines. |
140+
| `%o` | Pretty-print an Object all on a single line. |
141+
| `%%` | Single percent sign. Does not consume an argument. |
142+
143+
Other format specifiers supported by Node's `util.format` (`%s`, `%d`, `%j`, …) and the browser console pass through to the underlying logger.
144+
145+
#### Custom formatters
146+
147+
Register custom formatters via the `formatters` option. For example, to render a `Buffer` as hex with `%h`:
148+
149+
```ts
150+
const debug = createDebug('foo', {
151+
formatters: {
152+
h: (v) => v.toString('hex'),
153+
},
154+
})
155+
156+
debug('this is hex: %h', Buffer.from('hello world'))
157+
// foo this is hex: 68656c6c6f20776f726c64 +0ms
158+
```
159+
160+
### Dynamic enable / disable
161+
162+
You can toggle namespaces at runtime:
163+
164+
```ts
165+
import { disable, enable, enabled, namespaces } from 'obug'
166+
167+
enable('app:*')
168+
console.log(enabled('app:server')) // true
169+
170+
const previous = disable()
171+
console.log(enabled('app:server')) // false
172+
173+
// Restore later
174+
enable(previous)
175+
```
176+
177+
`namespaces()` returns the string of currently enabled namespaces. Note that calling `enable()` overrides the value initially read from `DEBUG`.
178+
179+
### Checking whether a debugger is enabled
180+
181+
Guard expensive work behind the `enabled` property:
182+
183+
```ts
184+
const debug = createDebug('http')
185+
186+
if (debug.enabled) {
187+
// expensive computation only when enabled
188+
}
189+
```
190+
191+
You can also force the state by assigning to `debug.enabled` directly.
192+
193+
### Custom output
194+
195+
By default, obug writes to `stderr` in Node.js and to the console in browsers. Override the `log` option to redirect output per-namespace:
196+
197+
```ts
198+
const log = createDebug('app:log', {
199+
log: console.log,
200+
})
201+
202+
const error = createDebug('app:error') // still goes to stderr
203+
```
204+
205+
### Environment variables (Node.js)
206+
207+
| Name | Purpose |
208+
| ------------------- | ----------------------------------------------- |
209+
| `DEBUG` | Enables/disables specific debugging namespaces. |
210+
| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
211+
| `DEBUG_COLORS` | Whether to use colors in the debug output. |
212+
| `DEBUG_DEPTH` | Object inspection depth. |
213+
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
214+
215+
Variables prefixed with `DEBUG_` are converted to camelCase keys on the options object passed to Node's [`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options) for the `%o` / `%O` formatters.
216+
75217
## Original Authors
76218

77219
As obug is a fork of debug with significant modifications, we would like to acknowledge the original authors:

0 commit comments

Comments
 (0)