gnodet commented on code in PR #13832:
URL: https://github.com/apache/maven/pull/13832#discussion_r4548357007


##########
impl/maven-logging/src/main/java/org/apache/maven/logging/impl/DefaultLogger.java:
##########
@@ -0,0 +1,419 @@
+/*
+ * 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.logging.impl;
+
+import java.lang.StackWalker.StackFrame;
+import java.time.Instant;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
+
+import org.apache.maven.api.MonotonicClock;
+import org.apache.maven.api.logging.LogEvent;
+import org.apache.maven.api.logging.LogEventBuilder;
+import org.apache.maven.api.logging.LogLevel;
+import org.apache.maven.api.logging.LogSink;
+import org.apache.maven.api.logging.Logger;
+import org.apache.maven.api.logging.LoggerManager;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Default implementation of both {@link Logger} (new fluent API)
+ * and the deprecated {@link org.apache.maven.api.plugin.Log} (backward 
compatibility).
+ * <p>
+ * Builds fully-structured {@link LogEvent} objects — including {@code 
projectId},
+ * {@code mojoId}, timestamp, and sequence number — and dispatches them to the
+ * current {@link LogSink} obtained from {@link LoggerManager#get()}.
+ * Level checks and sink lookup both go through the {@code LoggerManager},
+ * so the sink can be changed at runtime.
+ *
+ * @since 4.2.0
+ */
+@SuppressWarnings("deprecation")
+public class DefaultLogger implements Logger, org.apache.maven.api.plugin.Log {
+
+    private static final StackWalker WALKER = StackWalker.getInstance();
+    private static final String THIS_CLASS = DefaultLogger.class.getName();
+    private static final String BUILDER_CLASS = 
DefaultLogEventBuilder.class.getName();
+
+    private static final AtomicLong SEQUENCE = new AtomicLong();
+
+    private final String name;
+    private final String projectId;
+    private final String mojoId;
+
+    public DefaultLogger(String name) {
+        this(name, null, null);
+    }
+
+    public DefaultLogger(String name, String projectId, String mojoId) {
+        this.name = requireNonNull(name, "name");
+        this.projectId = projectId;
+        this.mojoId = mojoId;
+    }
+
+    // ---- org.apache.maven.api.logging.Logger ----
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    private boolean isLevelEnabled(LogLevel level) {
+        return LoggerManager.get().isLevelEnabled(name, level);
+    }
+
+    @Override
+    public boolean isEnabled(LogLevel level) {
+        return isLevelEnabled(level);
+    }
+
+    @Override
+    public LogEventBuilder atTrace() {
+        return isLevelEnabled(LogLevel.TRACE)
+                ? new DefaultLogEventBuilder(this, LogLevel.TRACE)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atDebug() {
+        return isLevelEnabled(LogLevel.DEBUG)
+                ? new DefaultLogEventBuilder(this, LogLevel.DEBUG)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atInfo() {
+        return isLevelEnabled(LogLevel.INFO)
+                ? new DefaultLogEventBuilder(this, LogLevel.INFO)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atWarn() {
+        return isLevelEnabled(LogLevel.WARN)
+                ? new DefaultLogEventBuilder(this, LogLevel.WARN)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atError() {
+        return isLevelEnabled(LogLevel.ERROR)
+                ? new DefaultLogEventBuilder(this, LogLevel.ERROR)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder at(LogLevel level) {
+        return isLevelEnabled(level) ? new DefaultLogEventBuilder(this, level) 
: NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public DefaultLogger child(String childName) {
+        requireNonNull(childName, "childName");
+        return new DefaultLogger(name + "." + childName, projectId, mojoId);
+    }
+
+    // ---- Internal dispatch ----
+
+    void doLog(LogLevel level, String message, Throwable throwable) {
+        doLog(level, message, null, null, null, throwable, null, null);
+    }
+
+    @SuppressWarnings("deprecation") // Thread.getId()
+    void doLog(
+            LogLevel level,
+            String message,
+            String messageTemplate,
+            Object[] messageArgs,
+            Map<String, Object> keyValues,
+            Throwable throwable) {
+        doLog(level, message, messageTemplate, messageArgs, keyValues, 
throwable, null, null);
+    }
+
+    @SuppressWarnings({"deprecation", "checkstyle:ParameterNumber"}) // 
Thread.getId()
+    void doLog(
+            LogLevel level,
+            String message,
+            String messageTemplate,
+            Object[] messageArgs,
+            Map<String, Object> keyValues,
+            Throwable throwable,
+            String projectIdOverride,
+            String mojoIdOverride) {
+        LoggerManager mgr = LoggerManager.get();
+        LogSink sink = mgr.getLogSink();
+        if (sink == null) {
+            return;
+        }
+
+        Instant timestamp = MonotonicClock.now();
+        long seq = SEQUENCE.getAndIncrement();
+
+        String sourceClass = null;
+        String sourceMethod = null;
+        long threadId = -1;
+        String threadName = null;
+
+        if (sink.needsMetadata()) {
+            sourceClass = name;
+            sourceMethod = resolveCallerMethod();
+            threadId = Thread.currentThread().getId();

Review Comment:
   Acknowledged — Thread.getId() is correct for the JDK 17 baseline. 
@SuppressWarnings and a TODO comment document the intent to switch to 
threadId() when baseline reaches JDK 19.



##########
impl/maven-logging/src/main/java/org/apache/maven/logging/impl/DefaultLogger.java:
##########
@@ -0,0 +1,419 @@
+/*
+ * 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.logging.impl;
+
+import java.lang.StackWalker.StackFrame;
+import java.time.Instant;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
+
+import org.apache.maven.api.MonotonicClock;
+import org.apache.maven.api.logging.LogEvent;
+import org.apache.maven.api.logging.LogEventBuilder;
+import org.apache.maven.api.logging.LogLevel;
+import org.apache.maven.api.logging.LogSink;
+import org.apache.maven.api.logging.Logger;
+import org.apache.maven.api.logging.LoggerManager;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Default implementation of both {@link Logger} (new fluent API)
+ * and the deprecated {@link org.apache.maven.api.plugin.Log} (backward 
compatibility).
+ * <p>
+ * Builds fully-structured {@link LogEvent} objects — including {@code 
projectId},
+ * {@code mojoId}, timestamp, and sequence number — and dispatches them to the
+ * current {@link LogSink} obtained from {@link LoggerManager#get()}.
+ * Level checks and sink lookup both go through the {@code LoggerManager},
+ * so the sink can be changed at runtime.
+ *
+ * @since 4.2.0
+ */
+@SuppressWarnings("deprecation")
+public class DefaultLogger implements Logger, org.apache.maven.api.plugin.Log {
+
+    private static final StackWalker WALKER = StackWalker.getInstance();
+    private static final String THIS_CLASS = DefaultLogger.class.getName();
+    private static final String BUILDER_CLASS = 
DefaultLogEventBuilder.class.getName();
+
+    private static final AtomicLong SEQUENCE = new AtomicLong();
+
+    private final String name;
+    private final String projectId;
+    private final String mojoId;
+
+    public DefaultLogger(String name) {
+        this(name, null, null);
+    }
+
+    public DefaultLogger(String name, String projectId, String mojoId) {
+        this.name = requireNonNull(name, "name");
+        this.projectId = projectId;
+        this.mojoId = mojoId;
+    }
+
+    // ---- org.apache.maven.api.logging.Logger ----
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    private boolean isLevelEnabled(LogLevel level) {
+        return LoggerManager.get().isLevelEnabled(name, level);
+    }
+
+    @Override
+    public boolean isEnabled(LogLevel level) {
+        return isLevelEnabled(level);
+    }
+
+    @Override
+    public LogEventBuilder atTrace() {
+        return isLevelEnabled(LogLevel.TRACE)
+                ? new DefaultLogEventBuilder(this, LogLevel.TRACE)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atDebug() {
+        return isLevelEnabled(LogLevel.DEBUG)
+                ? new DefaultLogEventBuilder(this, LogLevel.DEBUG)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atInfo() {
+        return isLevelEnabled(LogLevel.INFO)
+                ? new DefaultLogEventBuilder(this, LogLevel.INFO)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atWarn() {
+        return isLevelEnabled(LogLevel.WARN)
+                ? new DefaultLogEventBuilder(this, LogLevel.WARN)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder atError() {
+        return isLevelEnabled(LogLevel.ERROR)
+                ? new DefaultLogEventBuilder(this, LogLevel.ERROR)
+                : NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public LogEventBuilder at(LogLevel level) {
+        return isLevelEnabled(level) ? new DefaultLogEventBuilder(this, level) 
: NopLogEventBuilder.INSTANCE;
+    }
+
+    @Override
+    public DefaultLogger child(String childName) {
+        requireNonNull(childName, "childName");
+        return new DefaultLogger(name + "." + childName, projectId, mojoId);
+    }
+
+    // ---- Internal dispatch ----
+
+    void doLog(LogLevel level, String message, Throwable throwable) {
+        doLog(level, message, null, null, null, throwable, null, null);
+    }
+
+    @SuppressWarnings("deprecation") // Thread.getId()
+    void doLog(
+            LogLevel level,
+            String message,
+            String messageTemplate,
+            Object[] messageArgs,
+            Map<String, Object> keyValues,
+            Throwable throwable) {
+        doLog(level, message, messageTemplate, messageArgs, keyValues, 
throwable, null, null);
+    }
+
+    @SuppressWarnings({"deprecation", "checkstyle:ParameterNumber"}) // 
Thread.getId()
+    void doLog(
+            LogLevel level,
+            String message,
+            String messageTemplate,
+            Object[] messageArgs,
+            Map<String, Object> keyValues,
+            Throwable throwable,
+            String projectIdOverride,
+            String mojoIdOverride) {
+        LoggerManager mgr = LoggerManager.get();
+        LogSink sink = mgr.getLogSink();
+        if (sink == null) {
+            return;
+        }
+
+        Instant timestamp = MonotonicClock.now();
+        long seq = SEQUENCE.getAndIncrement();
+
+        String sourceClass = null;
+        String sourceMethod = null;
+        long threadId = -1;
+        String threadName = null;
+
+        if (sink.needsMetadata()) {
+            sourceClass = name;
+            sourceMethod = resolveCallerMethod();
+            threadId = Thread.currentThread().getId();

Review Comment:
   Acknowledged — Thread.getId() is correct for the JDK 17 baseline. 
@SuppressWarnings and a TODO comment document the intent to switch to 
threadId() when baseline reaches JDK 19.



##########
impl/maven-logging/src/main/java/org/apache/maven/logging/impl/DefaultLogEvent.java:
##########
@@ -0,0 +1,236 @@
+/*
+ * 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.logging.impl;
+
+import java.time.Instant;
+import java.util.Map;
+
+import org.apache.maven.api.logging.LogEvent;
+import org.apache.maven.api.logging.LogLevel;
+
+/**
+ * Immutable implementation of {@link LogEvent} with lazy message formatting.
+ * <p>
+ * When constructed with a {@code messageTemplate} and {@code messageArgs},
+ * the formatted {@link #message()} is computed lazily on first access.
+ * When constructed with a pre-formatted message, it is returned as-is.
+ *
+ * @since 4.2.0
+ */
+public final class DefaultLogEvent implements LogEvent {
+
+    private static final String PLACEHOLDER = "{}";
+
+    private final Instant timestamp;
+    private final LogLevel level;
+    private final String message;
+    private final String loggerName;
+    private final Throwable throwable;
+    private final String sourceClassName;
+    private final String sourceMethodName;
+    private final long threadId;
+    private final long sequenceNumber;
+    private final String threadName;
+    private final String projectId;
+    private final String mojoId;
+    private final Map<String, Object> keyValues;
+    private final String messageTemplate;
+    private final Object[] messageArgs;
+
+    private volatile String formattedMessage;
+
+    @SuppressWarnings("checkstyle:ParameterNumber")
+    public DefaultLogEvent(
+            Instant timestamp,
+            LogLevel level,
+            String message,
+            String loggerName,
+            Throwable throwable,
+            String sourceClassName,
+            String sourceMethodName,
+            long threadId,
+            long sequenceNumber,
+            String threadName,
+            String projectId,
+            String mojoId,
+            Map<String, Object> keyValues,
+            String messageTemplate,
+            Object[] messageArgs) {
+        this.timestamp = timestamp;
+        this.level = level;
+        this.message = message;
+        this.loggerName = loggerName;
+        this.throwable = throwable;
+        this.sourceClassName = sourceClassName;
+        this.sourceMethodName = sourceMethodName;
+        this.threadId = threadId;
+        this.sequenceNumber = sequenceNumber;
+        this.threadName = threadName;
+        this.projectId = projectId;
+        this.mojoId = mojoId;
+        this.keyValues = keyValues != null ? Map.copyOf(keyValues) : Map.of();
+        this.messageTemplate = messageTemplate;
+        this.messageArgs = messageArgs;

Review Comment:
   Fixed — messageArgs is now defensively copied via 
Collections.unmodifiableList(new ArrayList<>(messageArgs)), which handles null 
elements and satisfies the @Immutable contract.



-- 
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]

Reply via email to