merlimat opened a new pull request, #4766:
URL: https://github.com/apache/bookkeeper/pull/4766
## Summary
Add `withLoggerContext(Logger parentLogger)` to `CreateBuilder` and
`OpenBuilder` so an application that already has a per-request / per-tenant
slog `Logger` can pass it as the parent of the per-handle `Logger` that
bookkeeper builds. The `LedgerHandle`'s logger inherits the parent's context
via slog's `LoggerBuilder.ctx(Logger)`, then layers on the always-present
`ledgerId`.
```java
// Application's per-request logger, built once at the top of a request.
Logger requestLog = Logger.get(MyService.class).with()
.attr("requestId", requestId)
.attr("tenant", tenantId)
.build();
WriteHandle writer = bk.newCreateLedgerOp()
.withEnsembleSize(3).withWriteQuorumSize(3).withAckQuorumSize(2)
.withPassword(pw)
.withLoggerContext(requestLog) // ← new
.execute().get();
// Every log statement emitted by the writer (and the create-time machinery
// that produced it) now carries requestId, tenant, and ledgerId.
```
Passing a `Logger` (vs. a `Map<String, Object>`) avoids the need to extract
/ serialize attrs out of slog: the application's `Logger` is the natural source
of context.
## Implementation
- New `default` no-op method on the `@Public` / `@Unstable` `CreateBuilder`
and `OpenBuilder` interfaces — preserves source compatibility for any
out-of-tree implementor.
- `CreateBuilderImpl` and `OpenBuilderBase` store the parent `Logger` and
pass it through to `LedgerCreateOp` / `LedgerOpenOp`, which forward it to the
`LedgerHandle` / `LedgerHandleAdv` / `ReadOnlyLedgerHandle` constructor.
- New constructor overloads on the three handle types take a `Logger
parentLogger`; existing constructors delegate with `null`.
- `LedgerOpenOp`'s own contextual `log` is built with the same parent so log
events emitted during the open path also carry the attrs.
## Test plan
New `LoggerContextTest` covers:
- Compile-time API contract: `withLoggerContext(Logger)` on both builders is
chainable and returns the same builder type.
- Runtime: passing `null` is a no-op; non-null context map executes cleanly
through to `WriteHandle.getId()`.
- Open-on-non-existent-ledger end-to-end test under a log4j2 capturing
appender.
- Deterministic unit test of the slog
`LoggerBuilder.ctx(parent).attr("ledgerId", ...).build()` chain — verifies the
parent's attrs and `ledgerId` both appear in `event.getContextData()` on the
resulting `Logger`.
- [x] `mvn -pl bookkeeper-server compile checkstyle:check` clean
- [x] `LoggerContextTest` (5 tests) pass locally
- [x] Existing `BookKeeperBuildersTest` / `BookKeeperBuildersOpenLedgerTest`
(33 tests) unaffected
- [ ] Full CI matrix green
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]