gnodet opened a new issue, #14188:
URL: https://github.com/apache/maven/issues/14188
## Summary
`org.apache.maven.api.logging.LogEventBuilder` currently supports lazy
evaluation only via `log(Supplier<String>)`, which requires building the full
message string in the lambda. It lacks the ability to combine a **parameterized
format string** with **lazily-evaluated arguments**, unlike SLF4J 2.x's
`LoggingEventBuilder`.
## Proposed additions
### 1. `addArgument(Supplier<?>)` and `addArgument(Object)`
Mirroring SLF4J's `org.slf4j.spi.LoggingEventBuilder`:
```java
/**
* Adds a lazily-evaluated argument. The supplier is invoked only if the log
level is enabled
* (i.e. only if {@code atDebug()}/{@code atInfo()}/… was not
short-circuited).
*/
LogEventBuilder addArgument(@Nonnull Supplier<?> arg);
/**
* Adds an eagerly-evaluated argument. Provided for consistency so callers
can chain
* addArgument() calls uniformly regardless of whether individual args are
lazy or eager.
*/
LogEventBuilder addArgument(@Nullable Object arg);
```
This enables 3+ lazy args via chaining:
```java
logger.atDebug()
.addArgument(childModel::getId)
.addArgument(() -> repositories.stream()
.map(Object::toString)
.collect(Collectors.joining("\n", " ", "")))
.log("Merging repositories from {0}\n{1}");
```
### 2. Convenience `log()` overloads with `Supplier<?>` args (1- and 2-arg)
Default methods covering the common 1- and 2-argument cases, implemented in
terms of `addArgument`:
```java
default void log(@Nonnull String format, @Nonnull Supplier<?> arg) {
addArgument(arg).log(format);
}
default void log(@Nonnull String format, @Nonnull Supplier<?> arg1, @Nonnull
Supplier<?> arg2) {
addArgument(arg1).addArgument(arg2).log(format);
}
```
These allow the idiomatic single-expression form:
```java
logger.atDebug().log("Reading file model from {0}",
modelSource::getLocation);
```
## Why not just use `log(Supplier<String>)`?
The existing `log(Supplier<String>)` works but forces string concatenation
inside the lambda, losing the benefits of parameterized formatting (deferred
`toString()` calls, structured logging backends that can inspect args
individually). It also reads less naturally when only one of several arguments
is expensive to compute.
## Implementation notes
- `addArgument(Supplier<?>)` on `DefaultLogEvent`/`DefaultLogEventBuilder`
should call `supplier.get()` immediately at invocation time — the level check
already happened at `atDebug()`, so there is no need to defer further.
Arguments are accumulated into the existing args list, and `formatMessage()`
requires no changes.
- The 2 new `log()` default methods on the interface require no changes to
implementations.
## Related
Raised while reviewing PR #14177 (parameterized logging migration), where
`DefaultModelBuilder.java` has stream-collecting debug calls currently guarded
by `isDebugEnabled()` that could be simplified with this API.
--
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]