You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by zh...@apache.org on 2021/04/02 10:17:07 UTC

[hbase] branch branch-2.3 updated: HBASE-25696 Need to initialize SLF4JBridgeHandler in jul-to-slf4j for redirecting jul to slf4j (#3093) (#3112)

This is an automated email from the ASF dual-hosted git repository.

zhangduo pushed a commit to branch branch-2.3
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.3 by this push:
     new 01dc78d  HBASE-25696 Need to initialize SLF4JBridgeHandler in jul-to-slf4j for redirecting jul to slf4j (#3093) (#3112)
01dc78d is described below

commit 01dc78d52a3541f9e142cb862a0b0c97d73a656d
Author: Duo Zhang <zh...@apache.org>
AuthorDate: Fri Apr 2 17:30:43 2021 +0800

    HBASE-25696 Need to initialize SLF4JBridgeHandler in jul-to-slf4j for redirecting jul to slf4j (#3093) (#3112)
    
    Signed-off-by: Michael Stack <st...@apache.org>
---
 bin/hbase                                          |  2 +
 bin/hbase.cmd                                      |  3 +
 hbase-archetypes/hbase-client-project/pom.xml      | 10 +++
 .../hbase-shaded-client-project/pom.xml            | 10 +++
 .../apache/hadoop/hbase/logging/TestJul2Slf4j.java | 82 +++++++++++++++++++
 hbase-logging/pom.xml                              |  5 ++
 .../hbase/logging/JulToSlf4jInitializer.java       | 42 ++++++++++
 .../hbase-shaded-testing-util-tester/pom.xml       | 93 ++++++++++++----------
 pom.xml                                            |  1 +
 9 files changed, 205 insertions(+), 43 deletions(-)

diff --git a/bin/hbase b/bin/hbase
index d33749b..6654454 100755
--- a/bin/hbase
+++ b/bin/hbase
@@ -306,6 +306,8 @@ for f in "${HBASE_HOME}"/lib/client-facing-thirdparty/*.jar; do
     CLASSPATH="${CLASSPATH}:${f}"
   fi
 done
+# redirect java.util.logging to slf4j
+HBASE_OPTS="$HBASE_OPTS -Djava.util.logging.config.class=org.apache.hadoop.hbase.logging.JulToSlf4jInitializer"
 
 # default log directory & file
 if [ "$HBASE_LOG_DIR" = "" ]; then
diff --git a/bin/hbase.cmd b/bin/hbase.cmd
index a927227..3b56909 100644
--- a/bin/hbase.cmd
+++ b/bin/hbase.cmd
@@ -326,6 +326,9 @@ set HBASE_OPTS=%HBASE_OPTS% -Dhbase.home.dir="%HBASE_HOME%"
 set HBASE_OPTS=%HBASE_OPTS% -Dhbase.id.str="%HBASE_IDENT_STRING%"
 set HBASE_OPTS=%HBASE_OPTS% -XX:OnOutOfMemoryError="taskkill /F /PID %p"
 
+@rem redirect java.util.logging to slf4j
+set HBASE_OPTS=%HBASE_OPTS% -Djava.util.logging.config.class="org.apache.hadoop.hbase.logging.JulToSlf4jInitializer"
+
 if not defined HBASE_ROOT_LOGGER (
   set HBASE_ROOT_LOGGER=INFO,console
 )
diff --git a/hbase-archetypes/hbase-client-project/pom.xml b/hbase-archetypes/hbase-client-project/pom.xml
index 6618cc4..a9e92a8 100644
--- a/hbase-archetypes/hbase-client-project/pom.xml
+++ b/hbase-archetypes/hbase-client-project/pom.xml
@@ -55,6 +55,16 @@
     </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jul-to-slf4j</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <scope>runtime</scope>
     </dependency>
diff --git a/hbase-archetypes/hbase-shaded-client-project/pom.xml b/hbase-archetypes/hbase-shaded-client-project/pom.xml
index 661b2c0..c1507c6 100644
--- a/hbase-archetypes/hbase-shaded-client-project/pom.xml
+++ b/hbase-archetypes/hbase-shaded-client-project/pom.xml
@@ -61,6 +61,16 @@
     </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jul-to-slf4j</artifactId>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <scope>runtime</scope>
     </dependency>
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/logging/TestJul2Slf4j.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/logging/TestJul2Slf4j.java
new file mode 100644
index 0000000..cf654f5
--- /dev/null
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/logging/TestJul2Slf4j.java
@@ -0,0 +1,82 @@
+/**
+ * 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.hadoop.hbase.logging;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.io.IOException;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.testclassification.MiscTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.apache.log4j.Appender;
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.spi.LoggingEvent;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.ArgumentCaptor;
+
+/**
+ * This should be in the hbase-logging module but the {@link HBaseClassTestRule} is in hbase-common
+ * so we can only put the class in hbase-common module for now...
+ */
+@Category({ MiscTests.class, SmallTests.class })
+public class TestJul2Slf4j {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+    HBaseClassTestRule.forClass(TestJul2Slf4j.class);
+
+  static {
+    System.setProperty("java.util.logging.config.class", JulToSlf4jInitializer.class.getName());
+  }
+
+  private String loggerName = getClass().getName();
+
+  private Appender mockAppender;
+
+  @Before
+  public void setUp() {
+    mockAppender = mock(Appender.class);
+    LogManager.getRootLogger().addAppender(mockAppender);
+  }
+
+  @After
+  public void tearDown() {
+    LogManager.getRootLogger().removeAppender(mockAppender);
+  }
+
+  @Test
+  public void test() throws IOException {
+    java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName);
+    logger.info(loggerName);
+    ArgumentCaptor<LoggingEvent> captor = ArgumentCaptor.forClass(LoggingEvent.class);
+    verify(mockAppender, times(1)).doAppend(captor.capture());
+    LoggingEvent loggingEvent = captor.getValue();
+    assertThat(loggingEvent.getLevel(), is(Level.INFO));
+    assertEquals(loggerName, loggingEvent.getRenderedMessage());
+  }
+}
diff --git a/hbase-logging/pom.xml b/hbase-logging/pom.xml
index b81015a..83781f0 100644
--- a/hbase-logging/pom.xml
+++ b/hbase-logging/pom.xml
@@ -84,6 +84,11 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jul-to-slf4j</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <scope>provided</scope>
diff --git a/hbase-logging/src/main/java/org/apache/hadoop/hbase/logging/JulToSlf4jInitializer.java b/hbase-logging/src/main/java/org/apache/hadoop/hbase/logging/JulToSlf4jInitializer.java
new file mode 100644
index 0000000..e7b5fdd
--- /dev/null
+++ b/hbase-logging/src/main/java/org/apache/hadoop/hbase/logging/JulToSlf4jInitializer.java
@@ -0,0 +1,42 @@
+/**
+ * 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.hadoop.hbase.logging;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.logging.LogManager;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.slf4j.bridge.SLF4JBridgeHandler;
+
+/**
+ * Setup {@link SLF4JBridgeHandler}.
+ * <p/>
+ * Set the system property {@code java.util.logging.config.class} to this class to initialize the
+ * direction for java.util.logging to slf4j.
+ */
+@InterfaceAudience.Private
+public class JulToSlf4jInitializer {
+
+  private static final String PROPERTIES = "handlers=" + SLF4JBridgeHandler.class.getName();
+
+  public JulToSlf4jInitializer() throws IOException {
+    LogManager.getLogManager()
+      .readConfiguration(new ByteArrayInputStream(PROPERTIES.getBytes(StandardCharsets.UTF_8)));
+  }
+}
diff --git a/hbase-shaded/hbase-shaded-testing-util-tester/pom.xml b/hbase-shaded/hbase-shaded-testing-util-tester/pom.xml
index 9465c02..c6df436 100644
--- a/hbase-shaded/hbase-shaded-testing-util-tester/pom.xml
+++ b/hbase-shaded/hbase-shaded-testing-util-tester/pom.xml
@@ -1,6 +1,6 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <!--
       /**
        * Licensed to the Apache Software Foundation (ASF) under one
@@ -20,49 +20,56 @@
        * limitations under the License.
        */
       -->
-    <modelVersion>4.0.0</modelVersion>
+  <modelVersion>4.0.0</modelVersion>
 
-    <parent>
-        <groupId>org.apache.hbase</groupId>
-        <artifactId>hbase-build-configuration</artifactId>
-        <version>2.3.6-SNAPSHOT</version>
-        <relativePath>../../hbase-build-configuration</relativePath>
-    </parent>
+  <parent>
+    <groupId>org.apache.hbase</groupId>
+    <artifactId>hbase-build-configuration</artifactId>
+    <version>2.3.6-SNAPSHOT</version>
+    <relativePath>../../hbase-build-configuration</relativePath>
+  </parent>
 
-    <artifactId>hbase-shaded-testing-util-tester</artifactId>
-    <name>Apache HBase - Shaded - Testing Util Tester</name>
-    <description>Ensures that hbase-shaded-testing-util works with hbase-shaded-client.</description>
+  <artifactId>hbase-shaded-testing-util-tester</artifactId>
+  <name>Apache HBase - Shaded - Testing Util Tester</name>
+  <description>Ensures that hbase-shaded-testing-util works with hbase-shaded-client.</description>
 
-    <dependencies>
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <scope>test</scope>
-        </dependency>
-
-        <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-log4j12</artifactId>
-            <scope>test</scope>
-        </dependency>
-
-        <dependency>
-            <groupId>org.apache.hbase</groupId>
-            <artifactId>hbase-shaded-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.hbase</groupId>
-            <artifactId>hbase-shaded-testing-util</artifactId>
-            <version>${project.version}</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.codehaus.jackson</groupId>
-            <artifactId>jackson-mapper-asl</artifactId>
-            <version>1.9.13</version>
-            <scope>test</scope>
-        </dependency>
-    </dependencies>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jul-to-slf4j</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.hbase</groupId>
+      <artifactId>hbase-shaded-client</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.hbase</groupId>
+      <artifactId>hbase-shaded-testing-util</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.jackson</groupId>
+      <artifactId>jackson-mapper-asl</artifactId>
+      <version>1.9.13</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
 
 </project>
diff --git a/pom.xml b/pom.xml
index 92b2a71..4cc5aee 100755
--- a/pom.xml
+++ b/pom.xml
@@ -587,6 +587,7 @@
             <systemPropertyVariables>
               <test.build.classes>${test.build.classes}</test.build.classes>
               <java.io.tmpdir>${test.tmp.dir}</java.io.tmpdir>
+              <java.util.logging.config.class>org.apache.hadoop.hbase.logging.JulToSlf4jInitializer</java.util.logging.config.class>
             </systemPropertyVariables>
             <excludes>
               <!-- users can add -D option to skip particular test classes