Skip to content

Commit 0379d3f

Browse files
committed
HBASE-25696 Need to initialize SLF4JBridgeHandler in jul-to-slf4j for redirecting jul to slf4j (#3093)
Signed-off-by: Michael Stack <stack@apache.org>
1 parent 1d3ea38 commit 0379d3f

6 files changed

Lines changed: 135 additions & 0 deletions

File tree

bin/hbase

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ for f in "${HBASE_HOME}"/lib/client-facing-thirdparty/*.jar; do
306306
CLASSPATH="${CLASSPATH}:${f}"
307307
fi
308308
done
309+
# redirect java.util.logging to slf4j
310+
HBASE_OPTS="$HBASE_OPTS -Djava.util.logging.config.class=org.apache.hadoop.hbase.logging.JulToSlf4jInitializer"
309311

310312
# default log directory & file
311313
if [ "$HBASE_LOG_DIR" = "" ]; then

bin/hbase.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ set HBASE_OPTS=%HBASE_OPTS% -Dhbase.home.dir="%HBASE_HOME%"
326326
set HBASE_OPTS=%HBASE_OPTS% -Dhbase.id.str="%HBASE_IDENT_STRING%"
327327
set HBASE_OPTS=%HBASE_OPTS% -XX:OnOutOfMemoryError="taskkill /F /PID %p"
328328

329+
@rem redirect java.util.logging to slf4j
330+
set HBASE_OPTS=%HBASE_OPTS% -Djava.util.logging.config.class="org.apache.hadoop.hbase.logging.JulToSlf4jInitializer"
331+
329332
if not defined HBASE_ROOT_LOGGER (
330333
set HBASE_ROOT_LOGGER=INFO,console
331334
)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.logging;
19+
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
26+
27+
import java.io.IOException;
28+
import org.apache.hadoop.hbase.HBaseClassTestRule;
29+
import org.apache.hadoop.hbase.testclassification.MiscTests;
30+
import org.apache.hadoop.hbase.testclassification.SmallTests;
31+
import org.apache.log4j.Appender;
32+
import org.apache.log4j.Level;
33+
import org.apache.log4j.LogManager;
34+
import org.apache.log4j.spi.LoggingEvent;
35+
import org.junit.After;
36+
import org.junit.Before;
37+
import org.junit.ClassRule;
38+
import org.junit.Test;
39+
import org.junit.experimental.categories.Category;
40+
import org.mockito.ArgumentCaptor;
41+
42+
/**
43+
* This should be in the hbase-logging module but the {@link HBaseClassTestRule} is in hbase-common
44+
* so we can only put the class in hbase-common module for now...
45+
*/
46+
@Category({ MiscTests.class, SmallTests.class })
47+
public class TestJul2Slf4j {
48+
49+
@ClassRule
50+
public static final HBaseClassTestRule CLASS_RULE =
51+
HBaseClassTestRule.forClass(TestJul2Slf4j.class);
52+
53+
static {
54+
System.setProperty("java.util.logging.config.class", JulToSlf4jInitializer.class.getName());
55+
}
56+
57+
private String loggerName = getClass().getName();
58+
59+
private Appender mockAppender;
60+
61+
@Before
62+
public void setUp() {
63+
mockAppender = mock(Appender.class);
64+
LogManager.getRootLogger().addAppender(mockAppender);
65+
}
66+
67+
@After
68+
public void tearDown() {
69+
LogManager.getRootLogger().removeAppender(mockAppender);
70+
}
71+
72+
@Test
73+
public void test() throws IOException {
74+
java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName);
75+
logger.info(loggerName);
76+
ArgumentCaptor<LoggingEvent> captor = ArgumentCaptor.forClass(LoggingEvent.class);
77+
verify(mockAppender, times(1)).doAppend(captor.capture());
78+
LoggingEvent loggingEvent = captor.getValue();
79+
assertThat(loggingEvent.getLevel(), is(Level.INFO));
80+
assertEquals(loggerName, loggingEvent.getRenderedMessage());
81+
}
82+
}

hbase-logging/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
<artifactId>slf4j-log4j12</artifactId>
8484
<scope>test</scope>
8585
</dependency>
86+
<dependency>
87+
<groupId>org.slf4j</groupId>
88+
<artifactId>jul-to-slf4j</artifactId>
89+
<scope>provided</scope>
90+
</dependency>
8691
<dependency>
8792
<groupId>log4j</groupId>
8893
<artifactId>log4j</artifactId>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.logging;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.logging.LogManager;
24+
import org.apache.yetus.audience.InterfaceAudience;
25+
import org.slf4j.bridge.SLF4JBridgeHandler;
26+
27+
/**
28+
* Setup {@link SLF4JBridgeHandler}.
29+
* <p/>
30+
* Set the system property {@code java.util.logging.config.class} to this class to initialize the
31+
* direction for java.util.logging to slf4j.
32+
*/
33+
@InterfaceAudience.Private
34+
public class JulToSlf4jInitializer {
35+
36+
private static final String PROPERTIES = "handlers=" + SLF4JBridgeHandler.class.getName();
37+
38+
public JulToSlf4jInitializer() throws IOException {
39+
LogManager.getLogManager()
40+
.readConfiguration(new ByteArrayInputStream(PROPERTIES.getBytes(StandardCharsets.UTF_8)));
41+
}
42+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@
595595
<systemPropertyVariables>
596596
<test.build.classes>${test.build.classes}</test.build.classes>
597597
<java.io.tmpdir>${test.tmp.dir}</java.io.tmpdir>
598+
<java.util.logging.config.class>org.apache.hadoop.hbase.logging.JulToSlf4jInitializer</java.util.logging.config.class>
598599
</systemPropertyVariables>
599600
<excludes>
600601
<!-- users can add -D option to skip particular test classes

0 commit comments

Comments
 (0)