Skip to content

Commit 8484de8

Browse files
authored
docs: update debug logging documentation
Correct out of date component names and add section on using prefix logging.
1 parent 0f07e3d commit 8484de8

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

doc/GETTING_STARTED.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,28 @@ If you want to know more about libp2p peer discovery, you should read the follow
250250

251251
## Debugging
252252

253-
When running libp2p you may want to see what things are happening behind the scenes. You can see trace logs by setting the `DEBUG` environment variable when running in Node.js, and by setting `debug` as a localStorage item when running in the browser. Some examples:
253+
When running libp2p you may want to see what things are happening behind the scenes. You can see trace logs by setting the `DEBUG` environment variable when running in Node.js, and by setting `debug` as a localStorage item when running in the browser.
254+
255+
You may wish to start with trace logging, then narrow down the scope to just the components you are interested in.
256+
257+
Wildcards in components names are supported (`*`), and components can be excluded by prefxing their name with `-`.
258+
259+
Some examples:
254260

255261
### Node
256262

257263
```JavaScript
258264
# all libp2p debug logs
259265
DEBUG="libp2p:*" node my-script.js
260266

261-
# networking debug logs
262-
DEBUG="libp2p:tcp,libp2p:websockets,libp2p:webtransport,libp2p:kad-dht,libp2p:dialer" node my-script.js
267+
# all libp2p debug logs, maximum detail
268+
DEBUG="libp2p:*,*:trace" node my-script.js
269+
270+
# all libp2p debug logs including trace logs, exclude yamux
271+
DEBUG="libp2p:*,*:trace,-libp2p:yamux:*" node my-script.js
272+
273+
# networking debug logs (here TCP and WebSocket transports are configured)
274+
DEBUG="libp2p:tcp:*,libp2p:websockets:*,libp2p:kad-dht,libp2p:connection-manager:*" node my-script.js
263275
```
264276

265277
### Browser
@@ -269,9 +281,46 @@ DEBUG="libp2p:tcp,libp2p:websockets,libp2p:webtransport,libp2p:kad-dht,libp2p:di
269281
localStorage.setItem('debug', 'libp2p:*') // then refresh the page to ensure the libraries can read this when spinning up.
270282

271283
// networking debug logs
272-
localStorage.setItem('debug', 'libp2p:websockets,libp2p:webtransport,libp2p:kad-dht,libp2p:dialer')
284+
localStorage.setItem('debug', 'libp2p:websockets*,libp2p:webtransport*,libp2p:kad-dht*,libp2p:connection-manager*')
273285
```
274286

287+
### Prefix logger
288+
289+
If you are running multiple nodes in the same process (for example during a test run), separating their log output can be difficult.
290+
291+
In this situation you can use the `prefixLogger` from the `@libp2p/logger` module:
292+
293+
```ts
294+
import { prefixLogger } from '@libp2p/logger'
295+
import { createLibp2p } from 'libp2p'
296+
297+
const dialer = await createLibp2p({
298+
logger: prefixLogger('dialer')
299+
// ... other options
300+
})
301+
302+
const listener = await createLibp2p({
303+
logger: prefixLogger('listener')
304+
// ... other options
305+
})
306+
```
307+
308+
All logs will be prefixed by the string passed to the prefix logger, so later you can do:
309+
310+
```console
311+
DEBUG=dialer:*,listener:* node ./index.js
312+
listener:libp2p:transports adding transport @libp2p/tcp +0ms
313+
dialer:libp2p libp2p is starting +0ms
314+
listener:libp2p libp2p is starting +0ms
315+
dialer:libp2p:connection-manager started +0ms
316+
listener:libp2p:connection-manager started +0ms
317+
listener:libp2p:transports creating listener for @libp2p/tcp on /ip4/127.0.0.1/tcp/0 +6ms
318+
dialer:libp2p libp2p has started +7ms
319+
listener:libp2p libp2p has started +7ms
320+
```
321+
322+
...and apply component log filters via the `DEBUG` env var as normal, e.g. `DEBUG=listener:libp2p:tcp:*` to just see TCP transport logs for the listener node, etc.
323+
275324
## React Native
276325

277326
Libp2p can be used in React Native applications. However, there are some limitations and considerations to take into account as not all transports are supported and some of the underlying dependencies may not work as expected. There is on-going work to address these issues, particularly around the support of TCP. For a demo on how to use libp2p in a React Native application, see https://github.com/ipfs-shipyard/js-libp2p-react-native

0 commit comments

Comments
 (0)