gnodet-bot commented on code in PR #14062: URL: https://github.com/apache/maven/pull/14062#discussion_r4611939321
########## api/maven-api-log/src/main/java/org/apache/maven/api/logging/LogEventBuilder.java: ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api.logging; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; + +/** + * Fluent builder for a single log statement. + * <p> + * Instances are obtained from {@link Logger#atTrace()}, {@link Logger#atDebug()}, etc. + * A builder <em>must</em> be consumed in a single chained call; it must not be + * stored in a variable across statements, as implementations may reuse + * thread-local state for zero-allocation dispatch. + * <p> + * When the target level is disabled the {@code Logger} returns a shared + * {@code NopLogEventBuilder} singleton, making the entire fluent call chain a no-op + * with zero allocations. + * + * <pre>{@code + * log.atInfo() + * .withKeyValue("module", module.getId()) + * .log("Compiling {} source files", count); + * }</pre> + * + * @since 4.0.0 + */ +@Experimental +public interface LogEventBuilder { + + /** + * Attaches a structured key-value pair to this log event. + * + * @param key the key, never {@code null} + * @param value the string value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, String value); + + /** + * Attaches a structured key-value pair with a {@code long} value. + * + * @param key the key, never {@code null} + * @param value the numeric value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, long value); + + /** + * Attaches a structured key-value pair with a {@code boolean} value. + * + * @param key the key, never {@code null} + * @param value the boolean value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, boolean value); + + /** + * Attaches a {@link Throwable} to this log event. + * + * @param t the throwable, may be {@code null} (no-op) + * @return this builder + */ + @Nonnull + LogEventBuilder withThrowable(Throwable t); Review Comment: 💡 **Missing `@Nullable` annotation on `Throwable t` parameter** The Javadoc says "may be `{@code null}` (no-op)" but the parameter lacks `@Nullable`. Add it for consistency with the annotation-based nullability contract used throughout the API: ```suggestion LogEventBuilder withThrowable(@Nullable Throwable t); ``` ########## api/maven-api-log/src/main/java/org/apache/maven/api/logging/LogEventBuilder.java: ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api.logging; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; + +/** + * Fluent builder for a single log statement. + * <p> + * Instances are obtained from {@link Logger#atTrace()}, {@link Logger#atDebug()}, etc. + * A builder <em>must</em> be consumed in a single chained call; it must not be + * stored in a variable across statements, as implementations may reuse + * thread-local state for zero-allocation dispatch. + * <p> + * When the target level is disabled the {@code Logger} returns a shared + * {@code NopLogEventBuilder} singleton, making the entire fluent call chain a no-op + * with zero allocations. + * + * <pre>{@code + * log.atInfo() + * .withKeyValue("module", module.getId()) + * .log("Compiling {} source files", count); + * }</pre> + * + * @since 4.0.0 + */ +@Experimental +public interface LogEventBuilder { + + /** + * Attaches a structured key-value pair to this log event. + * + * @param key the key, never {@code null} + * @param value the string value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, String value); + + /** + * Attaches a structured key-value pair with a {@code long} value. + * + * @param key the key, never {@code null} + * @param value the numeric value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, long value); + + /** + * Attaches a structured key-value pair with a {@code boolean} value. + * + * @param key the key, never {@code null} + * @param value the boolean value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, boolean value); + + /** + * Attaches a {@link Throwable} to this log event. + * + * @param t the throwable, may be {@code null} (no-op) + * @return this builder + */ + @Nonnull + LogEventBuilder withThrowable(Throwable t); + + /** + * Overrides the project identifier for this event. + * <p> + * The default path resolves {@code projectId} from the current thread's + * {@code LogContext}; use this method only in the rare cases where the + * ThreadLocal context is incorrect (e.g. async hand-offs). + * + * @param projectId the project id, or {@code null} to clear + * @return this builder + */ + @Nonnull + LogEventBuilder withProjectId(String projectId); Review Comment: ⚠️ **Javadoc/implementation mismatch: `withProjectId()` / `withMojoId()` are silent no-ops** The Javadoc on `withProjectId()` (line 97) and `withMojoId()` (line 106) describes context-override semantics: > "Overrides the project identifier for this event. The default path resolves `projectId` from the current thread's `LogContext`..." But `DefaultLogEventBuilder.withProjectId(String)` and `withMojoId(String)` both simply `return this;` with no other effect. A plugin calling `atInfo().withProjectId("my:artifact").log("msg")` will get no context enrichment — silently. Either: 1. Implement the behavior (store the value, use it on dispatch), or 2. Update the Javadoc to clarify these are reserved for future structured-logging support and are currently ignored: _"Reserved for future structured logging support. The current SLF4J-backed implementation ignores this value; enrichment happens via `LogContext` ThreadLocals set by the container."_ ########## api/maven-api-log/src/main/java/org/apache/maven/api/logging/Logger.java: ########## @@ -0,0 +1,350 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api.logging; + +import java.util.function.Supplier; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.annotations.Provider; + +/** + * Unified logging interface for Maven plugins and CLI bootstrap code. + * <p> + * Unified interface that supports a fluent builder API, structured key-value + * pairs, and lazy message evaluation. Used by both mojo logging and CLI + * bootstrap logging. + * <p> + * Instances are obtained via dependency injection. A {@code @Singleton} bean + * may hold a long-lived {@code Logger} instance safely: context fields such as + * {@code projectId} and {@code mojoId} are resolved from a per-thread + * {@code LogContext} at event dispatch time, not at injection time. Review Comment: ⚠️ **`LogContext` is referenced but does not exist in this PR or anywhere in the `apache/maven` codebase** The class-level Javadoc on `Logger` (line 38) and the `withProjectId()` Javadoc in `LogEventBuilder` both reference `LogContext`, describing it as the per-thread mechanism for `projectId`/`mojoId` resolution. But `LogContext` is not defined anywhere in this PR or in the `maven-4.0.x` branch. Either add the `LogContext` class (the referenced ThreadLocal mechanism), or rewrite the Javadoc to reference what actually exists (e.g. `ProjectBuildLogAppender` and its ThreadLocal enrichment path). ########## api/maven-api-log/src/main/java/org/apache/maven/api/logging/LogEventBuilder.java: ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api.logging; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; + +/** + * Fluent builder for a single log statement. + * <p> + * Instances are obtained from {@link Logger#atTrace()}, {@link Logger#atDebug()}, etc. + * A builder <em>must</em> be consumed in a single chained call; it must not be + * stored in a variable across statements, as implementations may reuse + * thread-local state for zero-allocation dispatch. + * <p> + * When the target level is disabled the {@code Logger} returns a shared + * {@code NopLogEventBuilder} singleton, making the entire fluent call chain a no-op + * with zero allocations. + * + * <pre>{@code + * log.atInfo() + * .withKeyValue("module", module.getId()) + * .log("Compiling {} source files", count); + * }</pre> + * + * @since 4.0.0 + */ +@Experimental +public interface LogEventBuilder { + + /** + * Attaches a structured key-value pair to this log event. + * + * @param key the key, never {@code null} + * @param value the string value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, String value); + + /** + * Attaches a structured key-value pair with a {@code long} value. + * + * @param key the key, never {@code null} + * @param value the numeric value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, long value); + + /** + * Attaches a structured key-value pair with a {@code boolean} value. + * + * @param key the key, never {@code null} + * @param value the boolean value + * @return this builder + */ + @Nonnull + LogEventBuilder withKeyValue(@Nonnull String key, boolean value); + + /** + * Attaches a {@link Throwable} to this log event. + * + * @param t the throwable, may be {@code null} (no-op) + * @return this builder + */ + @Nonnull + LogEventBuilder withThrowable(Throwable t); + + /** + * Overrides the project identifier for this event. + * <p> + * The default path resolves {@code projectId} from the current thread's + * {@code LogContext}; use this method only in the rare cases where the + * ThreadLocal context is incorrect (e.g. async hand-offs). + * + * @param projectId the project id, or {@code null} to clear + * @return this builder + */ + @Nonnull + LogEventBuilder withProjectId(String projectId); Review Comment: ⚠️ **Javadoc/implementation mismatch: `withProjectId()` / `withMojoId()` are silent no-ops** The Javadoc on `withProjectId()` (line 97) and `withMojoId()` (line 106) describes context-override semantics: > "Overrides the project identifier for this event. The default path resolves `projectId` from the current thread's `LogContext`..." But `DefaultLogEventBuilder.withProjectId(String)` and `withMojoId(String)` both simply `return this;` with no other effect. A plugin calling `atInfo().withProjectId("my:artifact").log("msg")` will get no context enrichment — silently. Either: 1. Implement the behavior (store the value, use it on dispatch), or 2. Update the Javadoc to clarify these are reserved for future structured-logging support and are currently ignored: _"Reserved for future structured logging support. The current SLF4J-backed implementation ignores this value; enrichment happens via `LogContext` ThreadLocals set by the container."_ -- 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]
