You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "elharo (via GitHub)" <gi...@apache.org> on 2023/03/08 00:20:51 UTC

[GitHub] [maven] elharo commented on a diff in pull request #1041: [MNG-7724] adjust log level for the message when no known binding is found in Slf4jConfigurationFactory

elharo commented on code in PR #1041:
URL: https://github.com/apache/maven/pull/1041#discussion_r1128785518


##########
maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jConfigurationFactory.java:
##########
@@ -65,8 +67,14 @@ public static Slf4jConfiguration getConfiguration(ILoggerFactory loggerFactory)
             }
         } catch (IOException | ClassNotFoundException | IllegalAccessException | InstantiationException e) {
             e.printStackTrace();
+            hadException = true;
         }
 
-        return new UnsupportedSlf4jBindingConfiguration(slf4jBinding, supported);
+        return new UnsupportedSlf4jBindingConfiguration(
+                slf4jBinding,
+                supported,

Review Comment:
   warning seems the right level error. If someone is treating this as an error, that's their choice and problem, not ours. 



##########
maven-embedder/src/test/java/org/apache/maven/cli/logging/Slf4jConfigurationFactoryTest.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.cli.logging;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiConsumer;
+
+import org.apache.maven.cli.logging.impl.UnsupportedSlf4jBindingConfiguration;
+import org.junit.jupiter.api.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+
+import static java.util.Collections.singletonList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/**
+ * Validates some edge cases of Slf4jConfigurationFactory.
+ */
+public class Slf4jConfigurationFactoryTest {
+    @Test
+    void logInfoWhenNoBindingIsFound() {
+        final UnsupportedSlf4jBindingConfiguration configuration = assertInstanceOf(
+                UnsupportedSlf4jBindingConfiguration.class,
+                Slf4jConfigurationFactory.getConfiguration(new UnknownLoggerFactory()));
+        final BiConsumer<Logger, String> logHandler = findLogHandler(configuration);
+        final CapturingLogger logger = new CapturingLogger();
+        logHandler.accept(logger, "should be info");
+        assertEquals(singletonList("info"), logger.events);
+    }
+
+    @Test
+    void logWarnWhenNoBindingIsFoundButSomeFailedToLoad() throws IOException {
+        final Path fakeProvider = Paths.get("target/test-classes/META-INF/maven/slf4j-configuration.properties");
+        Files.createDirectories(fakeProvider.getParent());
+        Files.write(
+                fakeProvider,
+                ("org.apache.maven.cli.logging.Slf4jConfigurationFactoryTest.UnknownLoggerFactory will.fail.for.test")
+                        .getBytes(StandardCharsets.UTF_8));
+        try {
+            final UnsupportedSlf4jBindingConfiguration configuration = assertInstanceOf(
+                    UnsupportedSlf4jBindingConfiguration.class,
+                    Slf4jConfigurationFactory.getConfiguration(new UnknownLoggerFactory()));
+            final BiConsumer<Logger, String> logHandler = findLogHandler(configuration);
+            final CapturingLogger logger = new CapturingLogger();
+            logHandler.accept(logger, "should be warn");
+            assertEquals(singletonList("warn"), logger.events);
+        } finally {
+            Files.deleteIfExists(fakeProvider);
+        }
+    }
+
+    private BiConsumer<Logger, String> findLogHandler(final UnsupportedSlf4jBindingConfiguration configuration) {
+        try {
+            final Field log = configuration.getClass().getDeclaredField("log");
+            log.setAccessible(true);
+            return (BiConsumer<Logger, String>) log.get(configuration);
+        } catch (final Exception e) {

Review Comment:
   1, No need to mark this final
   2. Make exception specific if possible



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org