-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-25696 Need to initialize SLF4JBridgeHandler in jul-to-slf4j for…
- Loading branch information
Showing
9 changed files
with
205 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
hbase-common/src/test/java/org/apache/hadoop/hbase/logging/TestJul2Slf4j.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
hbase-logging/src/main/java/org/apache/hadoop/hbase/logging/JulToSlf4jInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters