Skip to content

Conversation

@joostjager
Copy link
Contributor

@joostjager joostjager commented Jan 12, 2026

Extend LogRecord with peer_id, channel_id, and payment_hash fields from
LDK's Record struct. These structured fields are now available to custom
LogWriter implementations and are automatically appended to log messages
by the built-in FileWriter and LogFacadeWriter.

  • Add peer_id, channel_id, payment_hash fields to LogRecord (both uniffi
    and non-uniffi versions)
  • Add LogContext struct with Display impl to format fields with truncated
    hex values, avoiding intermediate heap allocations
  • Update FileWriter and LogFacadeWriter to append context to messages
  • Update UDL bindings with new LogRecord fields
  • Add unit tests for LogContext and LogFacadeWriter

Closes #712

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jan 12, 2026

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@tnull tnull self-requested a review January 12, 2026 13:34
@joostjager joostjager force-pushed the structured-logging branch 2 times, most recently from 59d9bb1 to 22c37aa Compare January 12, 2026 14:35
Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

One comment, otherwise looks good.

While you're at it, mind also making the change to ldk-server as well? See https://github.com/lightningdevkit/ldk-server/blob/f3eaacd327d40fc8ee3fd7f6fbaccb04fa077434/ldk-server/src/util/logger.rs#L86-L129


let log = format!(
"{} {:<5} [{}:{}] {}\n",
"{} {:<5} [{}:{}] {}{}\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just appending it plain looks a bit odd for some messages:

Persistence of ChannelMonitorUpdate id 13 completed ch:d6487d p:02d87e

Can we maybe wrap the context in parentheses or brackets?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Parentheses added. Also made the implementation a bit more efficient by avoiding some heap allocs.

@joostjager joostjager requested a review from tnull January 13, 2026 08:03
/// Implements `Display` to format context fields (channel_id, peer_id, payment_hash) directly
/// into a formatter, avoiding intermediate heap allocations when used with `format_args!` or
/// `write!` macros.
pub struct LogContext<'a> {
Copy link
Collaborator

@tnull tnull Jan 13, 2026

Choose a reason for hiding this comment

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

Why does this need to be pub? Seems it's an internal utility only? When you drop it, please also drop the redundant doc comments here and on the Display impl.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For ldk-node users that have their own logger implementation, but also want to reuse the context fmting?

Copy link
Collaborator

@tnull tnull Jan 13, 2026

Choose a reason for hiding this comment

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

For ldk-node users that have their own logger implementation, but also want to reuse the context fmting?

Hmm, so do you plan to use it for LDK Server? If not, I'd say we can make it private as other users implementation might look differently. If we want to keep it pub for reuse in Server, let's still drop the docs on impl Display and also the entire pub fn new() which is just unnecessary (and to a degree an anti-pattern) if you have only pub struct fields anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, plan to use in server. Dropped new and moved Display docs.

@joostjager joostjager force-pushed the structured-logging branch 2 times, most recently from db45dff to 877bb46 Compare January 13, 2026 08:20
Extend LogRecord with peer_id, channel_id, and payment_hash fields from
LDK's Record struct. These structured fields are now available to custom
LogWriter implementations and are automatically appended to log messages
by the built-in FileWriter and LogFacadeWriter.

- Add peer_id, channel_id, payment_hash fields to LogRecord (both uniffi
  and non-uniffi versions)
- Add LogContext struct with Display impl to format fields with truncated
  hex values, avoiding intermediate heap allocations
- Update FileWriter and LogFacadeWriter to append context to messages
- Update UDL bindings with new LogRecord fields
- Add unit tests for LogContext and LogFacadeWriter

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

Update logging to include structured fields

3 participants