Skip to content

Commit

Permalink
feat: added multiaddr formatter to logging (libp2p#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad authored May 31, 2023
1 parent a09ff38 commit 1051708
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,16 @@
"docs": "aegir docs"
},
"dependencies": {
"@libp2p/interface-peer-id": "^2.0.0",
"debug": "^4.3.3",
"interface-datastore": "^8.0.0",
"multiformats": "^11.0.0"
"@libp2p/interface-peer-id": "^2.0.2",
"@multiformats/multiaddr": "^12.1.3",
"debug": "^4.3.4",
"interface-datastore": "^8.2.0",
"multiformats": "^11.0.2"
},
"devDependencies": {
"@libp2p/peer-id": "^2.0.3",
"@types/debug": "^4.1.7",
"aegir": "^38.1.7"
"aegir": "^38.1.7",
"uint8arrays": "^4.0.3"
}
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { base32 } from 'multiformats/bases/base32'
import { base64 } from 'multiformats/bases/base64'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { CID } from 'multiformats/cid'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Key } from 'interface-datastore'

// Add a formatter for converting to a base58 string
Expand Down Expand Up @@ -36,6 +37,11 @@ debug.formatters.k = (v: Key): string => {
return v == null ? 'undefined' : v.toString()
}

// Add a formatter for stringifying Multiaddrs
debug.formatters.ma = (v?: Multiaddr): string => {
return v == null ? 'undefined' : v.toString()
}

export interface Logger {
(formatter: any, ...args: any[]): void
error: (formatter: any, ...args: any[]) => void
Expand Down
65 changes: 65 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { expect } from 'aegir/chai'
import { logger } from '../src/index.js'
import debug from 'debug'
import { multiaddr } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as unint8ArrayToString } from 'uint8arrays/to-string'
import { base58btc } from 'multiformats/bases/base58'
import { base32 } from 'multiformats/bases/base32'
import { base64 } from 'multiformats/bases/base64'
import { Key } from 'interface-datastore'

describe('logger', () => {
it('creates a logger', () => {
Expand Down Expand Up @@ -52,4 +60,61 @@ describe('logger', () => {
expect(log).to.have.property('trace').that.is.a('function')
expect(log).to.have.nested.property('trace.enabled').that.is.true()
})

it('has all formatters', () => {
debug.enable('enabled-with-formatters')

expect(debug.formatters).to.have.property('b').that.is.a('function')
expect(debug.formatters).to.have.property('t').that.is.a('function')
expect(debug.formatters).to.have.property('m').that.is.a('function')
expect(debug.formatters).to.have.property('p').that.is.a('function')
expect(debug.formatters).to.have.property('c').that.is.a('function')
expect(debug.formatters).to.have.property('k').that.is.a('function')
expect(debug.formatters).to.have.property('ma').that.is.a('function')
})

it('test ma formatter', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')

expect(debug.formatters.ma(ma)).to.equal(ma.toString())
})

it('test peerId formatter', () => {
const peerId = peerIdFromString('QmZ8eiDPqQqWR17EPxiwCDgrKPVhCHLcyn6xSCNpFAdAZb')

expect(debug.formatters.p(peerId)).to.equal(peerId.toString())
})

it('test cid formatter', () => {
const peerId = peerIdFromString('QmZ8eiDPqQqWR17EPxiwCDgrKPVhCHLcyn6xSCNpFAdAZb')
const cid = peerId.toCID()

expect(debug.formatters.c(cid)).to.equal(cid.toString())
})

it('test base58 formatter', () => {
const buf = uint8ArrayFromString('12D3KooWbtp1AcgweFSArD7dbKWYpAr8MZR1tofwNwLFLjeNGLWa', 'base58btc')

expect(debug.formatters.b(buf)).to.equal(base58btc.baseEncode(buf))
})

it('test base32 formatter', () => {
const buf = uint8ArrayFromString('jbswy3dpfqqho33snrscc===', 'base32')

expect(debug.formatters.t(buf)).to.equal(base32.baseEncode(buf))
})

it('test base64 formatter', () => {
const buf = uint8ArrayFromString('12D3KooWbtp1AcgweFSArD7dbKWYpAr8MZR1tofwNwLFLjeNGLWa', 'base64')

expect(debug.formatters.m(buf)).to.equal(base64.baseEncode(buf))
})

it('test datastore key formatter', () => {
const buf = uint8ArrayFromString('jbswy3dpfqqho33snrscc===', 'base32')

const key = new Key('/' + unint8ArrayToString(buf, 'base32'), false)

expect(debug.formatters.k(key)).to.equal(key.toString())
})
})

0 comments on commit 1051708

Please sign in to comment.