From f327cd24825d9ce2f45a02fdb9b47c9735c847e0 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 7 Feb 2022 07:33:26 +0000 Subject: [PATCH] feat: add logger package (#158) 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. --- packages/libp2p-interfaces/src/index.ts | 5 - packages/libp2p-logger/CHANGELOG.md | 6 + packages/libp2p-logger/LICENSE | 4 + packages/libp2p-logger/LICENSE-APACHE | 5 + packages/libp2p-logger/LICENSE-MIT | 19 +++ packages/libp2p-logger/README.md | 51 ++++++++ packages/libp2p-logger/package.json | 142 +++++++++++++++++++++ packages/libp2p-logger/src/index.ts | 13 ++ packages/libp2p-logger/test/index.spec.ts | 12 ++ packages/libp2p-logger/tsconfig.json | 12 ++ packages/libp2p-pubsub/package.json | 3 +- packages/libp2p-pubsub/src/index.ts | 16 +-- packages/libp2p-pubsub/src/peer-streams.ts | 6 +- 13 files changed, 274 insertions(+), 20 deletions(-) create mode 100644 packages/libp2p-logger/CHANGELOG.md create mode 100644 packages/libp2p-logger/LICENSE create mode 100644 packages/libp2p-logger/LICENSE-APACHE create mode 100644 packages/libp2p-logger/LICENSE-MIT create mode 100644 packages/libp2p-logger/README.md create mode 100644 packages/libp2p-logger/package.json create mode 100644 packages/libp2p-logger/src/index.ts create mode 100644 packages/libp2p-logger/test/index.spec.ts create mode 100644 packages/libp2p-logger/tsconfig.json diff --git a/packages/libp2p-interfaces/src/index.ts b/packages/libp2p-interfaces/src/index.ts index bd4e2d37b..69b213e60 100644 --- a/packages/libp2p-interfaces/src/index.ts +++ b/packages/libp2p-interfaces/src/index.ts @@ -8,8 +8,3 @@ export interface Startable { stop: () => void | Promise isStarted: () => boolean } - -export interface Logger { - (...opts: any[]): void - error: (...opts: any[]) => void -} diff --git a/packages/libp2p-logger/CHANGELOG.md b/packages/libp2p-logger/CHANGELOG.md new file mode 100644 index 000000000..0560e31be --- /dev/null +++ b/packages/libp2p-logger/CHANGELOG.md @@ -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)) diff --git a/packages/libp2p-logger/LICENSE b/packages/libp2p-logger/LICENSE new file mode 100644 index 000000000..20ce483c8 --- /dev/null +++ b/packages/libp2p-logger/LICENSE @@ -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 diff --git a/packages/libp2p-logger/LICENSE-APACHE b/packages/libp2p-logger/LICENSE-APACHE new file mode 100644 index 000000000..14478a3b6 --- /dev/null +++ b/packages/libp2p-logger/LICENSE-APACHE @@ -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. diff --git a/packages/libp2p-logger/LICENSE-MIT b/packages/libp2p-logger/LICENSE-MIT new file mode 100644 index 000000000..72dc60d84 --- /dev/null +++ b/packages/libp2p-logger/LICENSE-MIT @@ -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. diff --git a/packages/libp2p-logger/README.md b/packages/libp2p-logger/README.md new file mode 100644 index 000000000..c1380c1b3 --- /dev/null +++ b/packages/libp2p-logger/README.md @@ -0,0 +1,51 @@ +# libp2p-logger + +> A logging component for use in js-libp2p components + +## Table of Contents + +- [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: +``` + +## 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. diff --git a/packages/libp2p-logger/package.json b/packages/libp2p-logger/package.json new file mode 100644 index 000000000..78f6873b6 --- /dev/null +++ b/packages/libp2p-logger/package.json @@ -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" + } +} diff --git a/packages/libp2p-logger/src/index.ts b/packages/libp2p-logger/src/index.ts new file mode 100644 index 000000000..82650a707 --- /dev/null +++ b/packages/libp2p-logger/src/index.ts @@ -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`) + }) +} diff --git a/packages/libp2p-logger/test/index.spec.ts b/packages/libp2p-logger/test/index.spec.ts new file mode 100644 index 000000000..4ea761a85 --- /dev/null +++ b/packages/libp2p-logger/test/index.spec.ts @@ -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') + }) +}) diff --git a/packages/libp2p-logger/tsconfig.json b/packages/libp2p-logger/tsconfig.json new file mode 100644 index 000000000..f296f9942 --- /dev/null +++ b/packages/libp2p-logger/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist", + "emitDeclarationOnly": false, + "module": "ES2020" + }, + "include": [ + "src", + "test" + ] +} diff --git a/packages/libp2p-pubsub/package.json b/packages/libp2p-pubsub/package.json index 4b5f8b1e4..debd75893 100644 --- a/packages/libp2p-pubsub/package.json +++ b/packages/libp2p-pubsub/package.json @@ -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", @@ -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" diff --git a/packages/libp2p-pubsub/src/index.ts b/packages/libp2p-pubsub/src/index.ts index 631bbbce6..3c82abaa8 100644 --- a/packages/libp2p-pubsub/src/index.ts +++ b/packages/libp2p-pubsub/src/index.ts @@ -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' @@ -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 } @@ -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 @@ -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 @@ -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 @@ -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)) @@ -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)) diff --git a/packages/libp2p-pubsub/src/peer-streams.ts b/packages/libp2p-pubsub/src/peer-streams.ts index 82be879b5..f5394761b 100644 --- a/packages/libp2p-pubsub/src/peer-streams.ts +++ b/packages/libp2p-pubsub/src/peer-streams.ts @@ -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' @@ -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