diff --git a/packages/libp2p-logger/README.md b/packages/libp2p-logger/README.md index c1380c1b3..9e3263896 100644 --- a/packages/libp2p-logger/README.md +++ b/packages/libp2p-logger/README.md @@ -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: +with this peer: 12D3Foo +with this base58btc: Qmfoo +with this base32: bafyfoo ``` ## License diff --git a/packages/libp2p-logger/package.json b/packages/libp2p-logger/package.json index 91549200a..13d732296 100644 --- a/packages/libp2p-logger/package.json +++ b/packages/libp2p-logger/package.json @@ -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", diff --git a/packages/libp2p-logger/src/index.ts b/packages/libp2p-logger/src/index.ts index 82650a707..8b00c6cc6 100644 --- a/packages/libp2p-logger/src/index.ts +++ b/packages/libp2p-logger/src/index.ts @@ -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