-
Notifications
You must be signed in to change notification settings - Fork 2.2k
build: add new PrefixLogger to create independent prefix loggers #10085
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
base: master
Are you sure you want to change the base?
Conversation
The `WithPrefix` method of the btclog/v2 logger will actually return a new copy of the underlying logger with the prefix: https://github.com/btcsuite/btclog/blob/e4cba5de17df2e1f729c00dae4abe3ba4e9cc7e8/v2/handler.go#L280-L310. This means that if the log level is changed in the original logger, this sub-logger won't actually be affected. To fix this, we create a new PrefixLogger struct, that emulates `WithPrefix`, but still routes messages through to the same backing logger that was passed in during creation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @Roasbeef, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request resolves a long-standing issue with the btclog/v2
library's WithPrefix
method, which inadvertently created isolated logger instances that did not respond to global log level adjustments. My solution introduces a custom PrefixLogger
that acts as a transparent wrapper, ensuring all prefixed log messages are routed through a shared base logger. This change centralizes log level control, making it possible to dynamically modify logging verbosity across all relevant components from a single point.
Highlights
- Improved Logging Behavior: I've introduced a new
PrefixLogger
inbuild/prefix_logger.go
. This custom logger wraps a basebtclog/v2.Logger
and correctly delegates all logging calls, ensuring that dynamic log level changes applied to the base logger are reflected in all instances ofPrefixLogger
that use it. This addresses a limitation wherebtclog/v2.WithPrefix
created independent logger copies that did not respect base logger level changes. - Migration of Existing Loggers: To leverage the corrected logging behavior, I've updated numerous core components across the codebase to use
build.NewPrefixLogger
instead of the originalbtclog/v2.WithPrefix
. This includes changes inchainio
,contractcourt
,htlcswitch
,lnwallet
,peer
,protofsm
,routing
, andwatchtower/wtclient
. - Comprehensive Testing: I've added a new test file,
build/prefix_logger_test.go
, which includes detailed tests. These tests demonstrate the original bug withbtclog/v2.WithPrefix
and thoroughly verify that the newPrefixLogger
correctly handles dynamic level changes, proper prefixing, and nested prefixes, ensuring the intended behavior.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a new PrefixLogger
to address an issue where log level changes were not propagating to sub-loggers created with WithPrefix
. The new implementation correctly delegates to a single backing logger, ensuring that log level changes are respected across all prefixed loggers.
The implementation is solid and the accompanying tests are comprehensive, verifying the fix and covering various use cases. The change is also applied consistently across the codebase.
My review focuses on ensuring the new code adheres to the project's style guide, particularly regarding line length limits for comments and function signatures. I've provided suggestions to address these minor style issues.
// PrefixLogger is a logger wrapper that adds a prefix to all log messages while | ||
// delegating to a base logger. This allows the base logger's level to be | ||
// changed dynamically and have it affect all PrefixLogger instances. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment block exceeds the 80-character line limit on line 11.1 Please re-wrap it to maintain code style consistency.
// PrefixLogger is a logger wrapper that adds a prefix to all log
// messages while delegating to a base logger. This allows the base
// logger's level to be changed dynamically and have it affect all
// PrefixLogger instances.
Style Guide References
Footnotes
// NewPrefixLogger creates a new PrefixLogger that adds the given prefix to all | ||
// log messages. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TestPrefixLoggerLevelChange tests that PrefixLogger correctly delegates to | ||
// the base logger and respects dynamic level changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment block exceeds the 80-character line limit on line 13.1 Please re-wrap it.
Style Guide References
// TestPrefixLoggerLevelChange tests that PrefixLogger correctly delegates to | |
// the base logger and respects dynamic level changes. | |
// TestPrefixLoggerLevelChange tests that PrefixLogger correctly delegates to the | |
// base logger and respects dynamic level changes. |
Footnotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pending CI, otherwise LGTM🌹
peer/brontide.go
Outdated
@@ -682,7 +682,7 @@ func NewBrontide(cfg Config) *Brontide { | |||
chanCloseMsgs: make(chan *closeMsg), | |||
resentChanSyncMsg: make(map[lnwire.ChannelID]struct{}), | |||
startReady: make(chan struct{}), | |||
log: peerLog.WithPrefix(logPrefix), | |||
log: NewPrefixLogger(peerLog, logPrefix), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like it can be dropped
The
WithPrefix
method of the btclog/v2 logger will actually return anew copy of the underlying logger with the prefix:
https://github.com/btcsuite/btclog/blob/e4cba5de17df2e1f729c00dae4abe3ba4e9cc7e8/v2/handler.go#L280-L310.
This means that if the log level is changed in the original logger, this
sub-logger won't actually be affected.
To fix this, we create a new PrefixLogger struct, that emulates
WithPrefix
, but still routes messages through to the same backinglogger that was passed in during creation.
Fixes #10084