Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
fix: add logging formatters (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Feb 7, 2022
1 parent a53f031 commit 2fa518c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/libp2p-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ const log = logger('libp2p:my:component:name')

log('something happened: %s', 'it was ok')
log.error('something bad happened: %o', err)

log('with this peer: %p', aPeerId)
log('and this base58btc: %b', aUint8Array)
log('and this base32: %t', aUint8Array)
```

```console
$ DEBUG=libp2p:* node index.js
something happened: it was ok
something bad happened: <stack trace>
with this peer: 12D3Foo
with this base58btc: Qmfoo
with this base32: bafyfoo
```

## License
Expand Down
3 changes: 2 additions & 1 deletion packages/libp2p-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@
},
"dependencies": {
"@libp2p/interfaces": "^1.0.0",
"debug": "^4.3.3"
"debug": "^4.3.3",
"multiformats": "^9.6.3"
},
"devDependencies": {
"@types/debug": "^4.1.7",
Expand Down
18 changes: 18 additions & 0 deletions packages/libp2p-logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import debug from 'debug'
import { base58btc } from 'multiformats/bases/base58'
import { base32 } from 'multiformats/bases/base32'
import type { PeerId } from '@libp2p/interfaces/peer-id'

// Add a formatter for converting to a base58 string
debug.formatters.b = (v: Uint8Array) => {
return base58btc.baseEncode(v)
}

// Add a formatter for converting to a base32 string
debug.formatters.t = (v: Uint8Array) => {
return base32.baseEncode(v)
}

// Add a formatter for stringifying peer ids
debug.formatters.p = (p: PeerId) => {
return p.toString(base58btc)
}

export interface Logger {
(formatter: any, ...args: any[]): void
Expand Down

0 comments on commit 2fa518c

Please sign in to comment.