Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Roasbeef
Copy link
Member

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.

Fixes #10084

Roasbeef added 2 commits July 16, 2025 17:12
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.
@Roasbeef Roasbeef added golang/build system Related to the go language and compiler bug fix labels Jul 17, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a 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 in build/prefix_logger.go. This custom logger wraps a base btclog/v2.Logger and correctly delegates all logging calls, ensuring that dynamic log level changes applied to the base logger are reflected in all instances of PrefixLogger that use it. This addresses a limitation where btclog/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 original btclog/v2.WithPrefix. This includes changes in chainio, contractcourt, htlcswitch, lnwallet, peer, protofsm, routing, and watchtower/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 with btclog/v2.WithPrefix and thoroughly verify that the new PrefixLogger 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

  1. 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.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +11 to +13
// 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.

Choose a reason for hiding this comment

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

medium

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

  1. The style guide requires that all lines be wrapped at 80 characters. (link)

Comment on lines +19 to +20
// NewPrefixLogger creates a new PrefixLogger that adds the given prefix to all
// log messages.

Choose a reason for hiding this comment

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

medium

This comment block exceeds the 80-character line limit on line 19.1 Please re-wrap it.

// NewPrefixLogger creates a new PrefixLogger that adds the given prefix to
// all log messages.

Style Guide References

Footnotes

  1. The style guide requires that all lines be wrapped at 80 characters. (link)

Comment on lines +13 to +14
// TestPrefixLoggerLevelChange tests that PrefixLogger correctly delegates to
// the base logger and respects dynamic level changes.

Choose a reason for hiding this comment

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

medium

This comment block exceeds the 80-character line limit on line 13.1 Please re-wrap it.

Style Guide References

Suggested change
// 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

  1. The style guide requires that all lines be wrapped at 80 characters. (link)

Copy link
Member

@yyforyongyu yyforyongyu left a 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),
Copy link
Member

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

@saubyk saubyk added this to the v0.20.0 milestone Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix golang/build system Related to the go language and compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug]: lncli debuglevel --level info not working in v0.19.x
3 participants