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


##########
impl/maven-logging-slf4j/src/main/java/org/apache/maven/logging/slf4j/MavenSimpleLoggerUtils.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.slf4j;
+
+import java.util.Map;
+
+import org.apache.maven.api.MonotonicClock;
+import org.apache.maven.api.logging.LogEvent;
+import org.apache.maven.api.logging.LogLevel;
+import org.apache.maven.logging.impl.DefaultLogEvent;
+import org.slf4j.spi.LocationAwareLogger;
+
+/**
+ * Static helpers for converting SLF4J events to the Maven logging API.
+ *
+ * @since 4.2.0
+ */
+public final class MavenSimpleLoggerUtils {
+
+    private MavenSimpleLoggerUtils() {}
+
+    /**
+     * Converts an SLF4J level integer to a {@link LogLevel}.
+     */
+    public static LogLevel toLogLevel(int level) {
+        return switch (level) {
+            case LocationAwareLogger.TRACE_INT -> LogLevel.TRACE;
+            case LocationAwareLogger.DEBUG_INT -> LogLevel.DEBUG;
+            case LocationAwareLogger.INFO_INT -> LogLevel.INFO;
+            case LocationAwareLogger.WARN_INT -> LogLevel.WARN;
+            default -> LogLevel.ERROR;
+        };
+    }
+
+    /**
+     * Creates a {@link LogEvent} from SLF4J-sourced parameters.
+     * The {@code messageTemplate} uses MessageFormat {@code {0}}-style 
placeholders
+     * (already converted from SLF4J's {@code {}} style by {@link 
Slf4jTemplateConverter}).
+     * SLF4J events have no source metadata (class/method).
+     * <p>
+     * {@code renderedLine} is the already-formatted output line (including 
colored level prefix)
+     * produced by the logger; it is stored as the pre-rendered message so 
that console sinks
+     * can emit it verbatim without re-formatting.
+     */
+    @SuppressWarnings("deprecation") // Thread.getId()
+    public static LogEvent toLogEvent(
+            int level,
+            String loggerName,
+            String messageTemplate,
+            Object[] messageArgs,
+            Throwable throwable,
+            String pluginId,
+            String renderedLine) {
+        return new DefaultLogEvent(
+                MonotonicClock.now(),
+                toLogLevel(level),
+                renderedLine,

Review Comment:
   ⚠️ **`message()` API contract violated — `renderedLine` is the full console 
line, not the semantic message**
   
   `renderedLine` = `formattedBuf.toString()` = the fully formatted output line 
produced by `MavenBaseLogger.formatLogPrefix()` + the message text. It contains 
the timestamp, thread info, and colored level prefix 
(`[\u001b[32mINFO\u001b[0m]`), _not_ just the log message.
   
   `LogEvent.message()` is documented as _"The log message, **without** level 
prefix or timestamp formatting."_ Storing the console line here violates that 
contract and makes `message()` return inconsistent content across log sources 
(SLF4J events get the full line; JUL and Maven-API events get the semantic 
text).
   
   Consumers break silently: `LookupInvoker.java` constructs `"[" + 
level.name() + "] " + event.message()` in the error diagnostics drain — with 
this change that path produces `[INFO] [INFO] loggerName: text` for 
SLF4J-sourced events.
   
   **Fix:** add a package-private or `impl`-only `renderedLine()` accessor to 
`DefaultLogEvent`, keep `message()` as the semantic text, and have 
`SimpleBuildEventListener` use `renderedLine()` with a fallback:
   
   ```suggestion
       public static LogEvent toLogEvent(
               int level,
               String loggerName,
               String messageTemplate,
               Object[] messageArgs,
               Throwable throwable,
               String pluginId) {
   ```
   
   (Remove the `renderedLine` parameter; store it via a separate 
`DefaultLogEvent.withRenderedLine(String)` builder step or a subclass field — 
do NOT pass it as `message`.)



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