gnodet commented on code in PR #13832: URL: https://github.com/apache/maven/pull/13832#discussion_r4548412162
########## impl/maven-logging-slf4j/src/main/java/org/apache/maven/logging/slf4j/MavenSimpleLogger.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.Arrays; +import java.util.Map; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.logging.LogLevel; +import org.apache.maven.api.logging.LogSink; +import org.apache.maven.api.logging.LoggerManager; +import org.apache.maven.logging.impl.DefaultLogEvent; +import org.apache.maven.logging.impl.DefaultLoggerManager; +import org.slf4j.Marker; +import org.slf4j.event.Level; +import org.slf4j.helpers.LegacyAbstractLogger; +import org.slf4j.spi.LocationAwareLogger; + +/** + * Thin SLF4J logger that dispatches events to the Maven unified logging API + * via {@link LoggerManager}. Level checks are delegated to + * {@link DefaultLoggerManager}; formatting is deferred to the {@link LogSink}. + * + * @since 3.5.0 Review Comment: Fixed — `@since` changed from `3.5.0` to `4.2.0`. ########## impl/maven-logging-slf4j/src/main/java/org/apache/maven/logging/slf4j/MavenLoggerFactory.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.Objects; +import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.maven.api.logging.LoggerManager; +import org.apache.maven.logging.impl.DefaultLoggerManager; +import org.slf4j.ILoggerFactory; +import org.slf4j.Logger; + +/** + * LogFactory for Maven which creates SLF4J loggers that delegate level checks + * and severity recording to the shared {@link DefaultLoggerManager}. + */ Review Comment: Fixed — added `@since 4.2.0` to class-level Javadoc. ########## impl/maven-logging-jul/src/test/java/org/apache/maven/logging/jul/MavenJulHandlerTest.java: ########## @@ -0,0 +1,189 @@ +/* + * 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.jul; + +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +import org.apache.maven.api.logging.LogEvent; +import org.apache.maven.api.logging.LogLevel; +import org.apache.maven.api.logging.LogSink; +import org.apache.maven.logging.impl.DefaultLoggerManager; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +@Timeout(10) +class MavenJulHandlerTest { + + private DefaultLoggerManager testManager; + + @BeforeEach + void installTestManager() { + testManager = new DefaultLoggerManager(); + testManager.setRootLoggerLevel(LogLevel.TRACE); + testManager.install(); + } + + @AfterEach + void clearTestManager() { + testManager.uninstall(); + testManager = null; + } + + @ParameterizedTest(name = "JUL {0} -> LogLevel.{1}") + @CsvSource({ + "FINEST, TRACE", + "FINER, DEBUG", + "FINE, DEBUG", + "CONFIG, INFO", + "INFO, INFO", + "WARNING, WARN", + "SEVERE, ERROR", + }) + void julLevelMapsToCorrectLogLevel(String julLevelName, String expectedLogLevel) { + Level julLevel = Level.parse(julLevelName); + LogLevel actual = MavenJulHandler.julLevelToLogLevel(julLevel); + assertEquals(LogLevel.valueOf(expectedLogLevel), actual); + } + + @Test + void finestMapsToTrace() { + assertEquals(LogLevel.TRACE, MavenJulHandler.julLevelToLogLevel(Level.FINEST)); + } + + @Test + void configMapsToInfo() { + assertEquals(LogLevel.INFO, MavenJulHandler.julLevelToLogLevel(Level.CONFIG)); + } + + @Test + void publishIsReentrantSafe() { + MavenJulHandler handler = new MavenJulHandler(); + LogRecord record = new LogRecord(Level.INFO, "nested"); + + MavenJulHandler.setInPublishForTest(true); + try { + handler.publish(record); + } finally { + MavenJulHandler.setInPublishForTest(false); + } + } Review Comment: Fixed — added `assertNull(captured.get())` happy-path assertion. ########## 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: No longer applicable — `MavenSimpleLoggerUtils.java` has been deleted as part of the SLF4J module slim-down. ########## its/core-it-suite/src/test/resources/gh-13522-logger-context/src/main/java/org/apache/maven/its/gh13522/CheckContextMojo.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.its.gh13522; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.logging.LogEvent; +import org.apache.maven.api.logging.Logger; +import org.apache.maven.api.logging.LoggerManager; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; + +/** + * Verifies that a DI-injected {@link Logger} carries the correct + * {@code projectId}, {@code mojoId}, and {@code pluginId} on each + * {@link LogEvent} it emits. + * + * <p>The mojo wraps the global {@link LoggerManager} sink, captures + * the first event it emits, extracts the context fields, and writes + * them to {@code target/log-context.properties} so the IT verifier + * can assert their values.</p> + */ +@Mojo(name = "check-context", defaultPhase = org.apache.maven.api.Lifecycle.Phase.VALIDATE, projectRequired = false) Review Comment: Fixed — `projectRequired = true` has been added to the `@Mojo` annotation. ########## its/core-it-suite/src/test/resources/gh-13522-logger-context/src/main/java/org/apache/maven/its/gh13522/CheckContextMojo.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.its.gh13522; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.logging.LogEvent; +import org.apache.maven.api.logging.Logger; +import org.apache.maven.api.logging.LoggerManager; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; + +/** + * Verifies that a DI-injected {@link Logger} carries the correct + * {@code projectId}, {@code mojoId}, and {@code pluginId} on each + * {@link LogEvent} it emits. + * + * <p>The mojo wraps the global {@link LoggerManager} sink, captures + * the first event it emits, extracts the context fields, and writes + * them to {@code target/log-context.properties} so the IT verifier + * can assert their values.</p> + */ +@Mojo(name = "check-context", defaultPhase = org.apache.maven.api.Lifecycle.Phase.VALIDATE, projectRequired = false) Review Comment: Fixed — `projectRequired = true` has been added to the `@Mojo` annotation. -- 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]
