Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add component logger #2198

Merged
merged 2 commits into from
Nov 7, 2023
Merged

feat: add component logger #2198

merged 2 commits into from
Nov 7, 2023

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Nov 4, 2023

When running multiple libp2p instances simultaneously it's often hard to work out which log messages come from which instance. This PR solves the problem.

  • Adds a ComponentLogger interface to libp2p which is an object that lets a component get a logger specific to that component.
  • Adds an optional logger config key to libp2p which is an implementation of ComponentLogger.
  • Adds three implementations of ComponentLogger
    • defaultLogger - this preserves existing logging behaviour
    • peerLogger - this prefixes all log lines with a truncated peer id
    • prefixLogger - this prefixes all log lines with an arbitrary string

libp2p uses the passed logger if one is configured, otherwise it uses defaultLogger to make this change backwards compatible.

The logger is added to the components map so libp2p components can accept it as part of their required components and use the forComponent function to create a component-specific logger.

It will take a bit of time to thread the use of ComponentLogger throughout the monorepo, but this can be done in follow-up PRs since the existing logging behaviour is maintained by the changes here.

For v1.x we may switch to peerLogger.

Refs #2105

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@achingbrain achingbrain marked this pull request as ready for review November 6, 2023 12:19
@achingbrain achingbrain requested a review from a team as a code owner November 6, 2023 12:19
@achingbrain achingbrain changed the title feat: add peer logger feat: add component logger Nov 6, 2023
Copy link
Member

@maschad maschad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very useful feature, great work. I left some comments and suggestions.

packages/logger/src/index.ts Outdated Show resolved Hide resolved
packages/logger/src/index.ts Outdated Show resolved Hide resolved
packages/libp2p/test/connection-manager/index.spec.ts Outdated Show resolved Hide resolved
packages/libp2p/src/libp2p.ts Show resolved Hide resolved
Adds a `ComponentLogger` to `@libp2p/logger` with implementations that
prefixes all log messages with a truncated peer id or an arbitrary
string.

When running multiple libp2p instances simultaneously it's often hard
to work out which log messages come from which instance. This will
solve this problem.

Refs #2105
@achingbrain achingbrain merged commit fb8a6f1 into master Nov 7, 2023
22 checks passed
@achingbrain achingbrain deleted the feat/add-peer-logger branch November 7, 2023 14:39
maschad pushed a commit to maschad/js-libp2p that referenced this pull request Nov 10, 2023
Adds a `ComponentLogger` to `@libp2p/logger` with implementations that
prefixes all log messages with a truncated peer id or an arbitrary
string.

When running multiple libp2p instances simultaneously it's often hard
to work out which log messages come from which instance. This will
solve this problem.

Refs libp2p#2105
achingbrain added a commit to ChainSafe/js-libp2p-yamux that referenced this pull request Nov 26, 2023
Refactors code to use the component logger from libp2p to allow
more flexible logging patterns.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378
achingbrain added a commit to ChainSafe/js-libp2p-yamux that referenced this pull request Nov 26, 2023
Refactors code to use the component logger from libp2p to allow
more flexible logging patterns.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378
achingbrain added a commit to achingbrain/js-libp2p-noise that referenced this pull request Nov 26, 2023
Refactors code to use the component logger from libp2p to allow more
flexible logging patterns.

Nb. adds a `NoiseComponents` interface separate from `NoiseInit` that
contains the `Metrics` instance - this is consistent with every other
libp2p module.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378
achingbrain added a commit to achingbrain/js-libp2p-noise that referenced this pull request Nov 29, 2023
Refactors code to use the component logger from libp2p to allow more
flexible logging patterns.

Nb. adds a `NoiseComponents` interface separate from `NoiseInit` that
contains the `Metrics` instance - this is consistent with every other
libp2p module.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378
achingbrain added a commit to achingbrain/js-libp2p-noise that referenced this pull request Nov 29, 2023
Refactors code to use the component logger from libp2p to allow more
flexible logging patterns.

Nb. adds a `NoiseComponents` interface separate from `NoiseInit` that
contains the `Metrics` instance - this is consistent with every other
libp2p module.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378
wemeetagain added a commit to ChainSafe/js-libp2p-noise that referenced this pull request Nov 29, 2023
* fix: yield uint8arraylists instead of concatenating buffers

In order to avoid unnecessary buffer copies, update to the new libp2p
connection encrypter API that lets connection encrypters consume/yield
lists of buffers instead of requiring them to be concatenated
before/after encryption/decryption.

* chore: fix tcp version

* feat: use libp2p component logger

Refactors code to use the component logger from libp2p to allow more
flexible logging patterns.

Nb. adds a `NoiseComponents` interface separate from `NoiseInit` that
contains the `Metrics` instance - this is consistent with every other
libp2p module.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378

* chore: fix linter errors

---------

Co-authored-by: Cayman <caymannava@gmail.com>
This was referenced Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants