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

Commit

Permalink
feat: add logger package (#158)
Browse files Browse the repository at this point in the history
Adds @libp2p/logger for use in libp2p components.

Currently wraps debug but lets us swap it out in future - for example
if it doesn't go to ESM in a reasonable timeframe.
  • Loading branch information
achingbrain authored Feb 7, 2022
1 parent 29afc7f commit f327cd2
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 20 deletions.
5 changes: 0 additions & 5 deletions packages/libp2p-interfaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ export interface Startable {
stop: () => void | Promise<void>
isStarted: () => boolean
}

export interface Logger {
(...opts: any[]): void
error: (...opts: any[]) => void
}
6 changes: 6 additions & 0 deletions packages/libp2p-logger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## @libp2p/tracked-map-v1.0.0 (2022-02-05)


### Features

* add tracked-map ([#156](https://github.com/libp2p/js-libp2p-interfaces/issues/156)) ([c17730f](https://github.com/libp2p/js-libp2p-interfaces/commit/c17730f8bca172db85507740eaba81b3cf514d04))
4 changes: 4 additions & 0 deletions packages/libp2p-logger/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/libp2p-logger/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/libp2p-logger/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
51 changes: 51 additions & 0 deletions packages/libp2p-logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# libp2p-logger <!-- omit in toc -->

> A logging component for use in js-libp2p components
## Table of Contents <!-- omit in toc -->

- [Description](#description)
- [Installation](#installation)
- [Example](#example)
- [License](#license)
- [Contribution](#contribution)

## Description

A map that reports it's size to the libp2p [Metrics](https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-interfaces/src/metrics#readme) system.

If metrics are disabled a regular map is used.

## Installation

```console
$ npm i @libp2p/logger
```

## Example

```JavaScript
import { logger } from '@libp2p/logger'

const log = logger('libp2p:my:component:name')

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

```console
$ DEBUG=libp2p:* node index.js
something happened: it was ok
something bad happened: <stack trace>
```

## License

Licensed under either of

* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
142 changes: 142 additions & 0 deletions packages/libp2p-logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"name": "@libp2p/logger",
"version": "0.0.0",
"description": "A logging component for use in js-libp2p components",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-logger#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-interfaces.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-interfaces/issues"
},
"keywords": [
"IPFS"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist/src",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"release": {
"branches": [
"master"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"releaseRules": [
{
"breaking": true,
"release": "major"
},
{
"revert": true,
"release": "patch"
},
{
"type": "feat",
"release": "minor"
},
{
"type": "fix",
"release": "patch"
},
{
"type": "chore",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
},
{
"type": "test",
"release": "patch"
},
{
"scope": "no-release",
"release": false
}
]
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"presetConfig": {
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Trivial Changes"
},
{
"type": "docs",
"section": "Trivial Changes"
},
{
"type": "test",
"section": "Tests"
}
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check dist/src/**/*.js",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/**/*.js",
"test:chrome": "npm run test -- -t browser",
"test:chrome-webworker": "npm run test -- -t webworker",
"test:firefox": "npm run test -- -t browser -- --browser firefox",
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
"test:node": "npm run test -- -t node --cov",
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/interfaces": "^1.0.0",
"debug": "^4.3.3"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"aegir": "^36.1.3"
}
}
13 changes: 13 additions & 0 deletions packages/libp2p-logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import debug from 'debug'

export interface Logger {
(formatter: any, ...args: any[]): void
error: (formatter: any, ...args: any[]) => void
enabled: boolean
}

export function logger (name: string): Logger {
return Object.assign(debug(name), {
error: debug(`${name}:error`)
})
}
12 changes: 12 additions & 0 deletions packages/libp2p-logger/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect } from 'aegir/utils/chai.js'
import { logger } from '../src/index.js'

describe('logger', () => {
it('creates a logger', () => {
const log = logger('hello')

expect(log).to.be.a('function')
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.property('enabled')
})
})
12 changes: 12 additions & 0 deletions packages/libp2p-logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"emitDeclarationOnly": false,
"module": "ES2020"
},
"include": [
"src",
"test"
]
}
3 changes: 1 addition & 2 deletions packages/libp2p-pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@
"dependencies": {
"@libp2p/crypto": "^0.22.2",
"@libp2p/interfaces": "^1.0.0",
"@libp2p/logger": "^0.0.0",
"@libp2p/peer-id": "^1.0.0",
"@libp2p/peer-id-factory": "^1.0.0",
"@libp2p/topology": "^1.0.0",
"@multiformats/multiaddr": "^10.1.1",
"debug": "^4.3.2",
"err-code": "^3.0.1",
"iso-random-stream": "^2.0.0",
"it-length-prefixed": "^6.0.1",
Expand All @@ -203,7 +203,6 @@
},
"devDependencies": {
"@types/bl": "^5.0.2",
"@types/debug": "^4.1.7",
"aegir": "^36.1.3",
"protobufjs": "^6.10.2",
"util": "^0.12.4"
Expand Down
16 changes: 7 additions & 9 deletions packages/libp2p-pubsub/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug from 'debug'
import { logger } from '@libp2p/logger'
import { EventEmitter } from 'events'
import errcode from 'err-code'
import { pipe } from 'it-pipe'
Expand All @@ -18,6 +18,7 @@ import {
} from './message/sign.js'
import type { PubSub, Message, StrictNoSign, StrictSign, PubsubOptions } from '@libp2p/interfaces/pubsub'
import type { Startable } from '@libp2p/interfaces'
import type { Logger } from '@libp2p/logger'

export interface TopicValidator { (topic: string, message: Message): Promise<void> }

Expand Down Expand Up @@ -62,7 +63,7 @@ export abstract class PubsubBaseProtocol extends EventEmitter implements PubSub,
public queue: Queue
public registrar: Registrar

protected log: debug.Debugger & { err: debug.Debugger }
protected log: Logger
protected multicodecs: string[]
protected _libp2p: any
private _registrarId: string | undefined
Expand All @@ -80,10 +81,7 @@ export abstract class PubsubBaseProtocol extends EventEmitter implements PubSub,
messageProcessingConcurrency = 10
} = props

this.log = Object.assign(debug(debugName), {
err: debug(`${debugName}:error`)
})

this.log = logger(debugName)
this.multicodecs = utils.ensureArray(multicodecs)
this._libp2p = libp2p
this.registrar = libp2p.registrar
Expand Down Expand Up @@ -189,7 +187,7 @@ export abstract class PubsubBaseProtocol extends EventEmitter implements PubSub,
const peer = this._addPeer(peerId, protocol)
await peer.attachOutboundStream(stream)
} catch (err: any) {
this.log.err(err)
this.log.error(err)
}

// Immediately send my own subscriptions to the newly established conn
Expand Down Expand Up @@ -320,7 +318,7 @@ export abstract class PubsubBaseProtocol extends EventEmitter implements PubSub,

await this._processRpcMessage(msg)
} catch (err: any) {
this.log.err(err)
this.log.error(err)
}
}))
.catch(err => this.log(err))
Expand Down Expand Up @@ -435,7 +433,7 @@ export abstract class PubsubBaseProtocol extends EventEmitter implements PubSub,
if ((peerStreams == null) || !peerStreams.isWritable) {
const msg = `Cannot send RPC to ${id} as there is no open stream to it available`

this.log.err(msg)
this.log.error(msg)
return
}
peerStreams.write(this._encodeRpc(rpc))
Expand Down
6 changes: 2 additions & 4 deletions packages/libp2p-pubsub/src/peer-streams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug from 'debug'
import { logger } from '@libp2p/logger'
import { EventEmitter } from 'events'
import * as lp from 'it-length-prefixed'
import { pushable } from 'it-pushable'
Expand All @@ -8,9 +8,7 @@ import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { MuxedStream } from '@libp2p/interfaces/stream-muxer'
import type { Pushable } from 'it-pushable'

const log = Object.assign(debug('libp2p-pubsub:peer-streams'), {
error: debug('libp2p-pubsub:peer-streams:err')
})
const log = logger('libp2p-pubsub:peer-streams')

export interface Options {
id: PeerId
Expand Down

0 comments on commit f327cd2

Please sign in to comment.