backport merge updates to main (#3341)

* backport merge updates to main

Signed-off-by: garyschulte <garyschulte@gmail.com>
This commit is contained in:
garyschulte
2022-01-27 22:17:21 -08:00
committed by GitHub
parent 5040533b88
commit b3e1b5f86c
6 changed files with 202 additions and 36 deletions

View File

@@ -48,6 +48,10 @@ public class Log4j2ConfiguratorUtil {
}
}
public static void setLevelDebug(final String loggerName) {
setLevel(loggerName, Level.DEBUG);
}
public static void setLevel(final String loggerName, final Level level) {
final LoggerContext loggerContext = getLoggerContext();
if (Strings.isEmpty(loggerName)) {

View File

@@ -0,0 +1,51 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.util;
import java.util.Arrays;
import java.util.function.Supplier;
import org.slf4j.Logger;
/**
* Static helper class to shim SLF4J with lambda parameter suppliers until the final release of
* SLF4J 2.0.
*/
public class Slf4jLambdaHelper {
// sonar code smell
private Slf4jLambdaHelper() {}
public static void debugLambda(
final Logger log, final String message, final Supplier<?>... params) {
if (log.isDebugEnabled()) {
log.debug(message, Arrays.stream(params).map(Supplier::get).toArray());
}
}
public static void traceLambda(
final Logger log, final String message, final Supplier<?>... params) {
if (log.isTraceEnabled()) {
log.trace(message, Arrays.stream(params).map(Supplier::get).toArray());
}
}
public static void warnLambda(
final Logger log, final String message, final Supplier<?>... params) {
if (log.isWarnEnabled()) {
log.warn(message, Arrays.stream(params).map(Supplier::get).toArray());
}
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.util;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hyperledger.besu.util.Slf4jLambdaHelper.debugLambda;
import static org.hyperledger.besu.util.Slf4jLambdaHelper.traceLambda;
import static org.hyperledger.besu.util.Slf4jLambdaHelper.warnLambda;
import java.util.ArrayDeque;
import java.util.function.Supplier;
import org.apache.logging.log4j.Level;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Slf4jLambdaHelperTest {
private static final Logger LOG = LoggerFactory.getLogger(Slf4jLambdaHelperTest.class);
private static final ArrayDeque<String> paramStack = new ArrayDeque<>();
@Before
public void paramSetup() {
paramStack.push("stuff");
paramStack.push("more stuff");
paramStack.push("last stuff");
}
@Test
public void smokeDebugLambda() {
Log4j2ConfiguratorUtil.setLevel(LOG.getName(), Level.WARN);
debugLambda(
LOG,
"blah",
(Supplier<String>)
() -> {
throw new RuntimeException("should not evaluate");
});
Log4j2ConfiguratorUtil.setLevelDebug(LOG.getName());
assertThat(paramStack.size()).isEqualTo(3);
debugLambda(LOG, "blah {}", paramStack::pop);
assertThat(paramStack.size()).isEqualTo(2);
debugLambda(LOG, "blah {} {}", paramStack::pop, paramStack::pop);
assertThat(paramStack.size()).isZero();
}
@Test
public void smokeTraceLambda() {
traceLambda(
LOG,
"blah",
(Supplier<String>)
() -> {
throw new RuntimeException("should not evaluate");
});
Log4j2ConfiguratorUtil.setLevel(LOG.getName(), Level.TRACE);
assertThat(paramStack.size()).isEqualTo(3);
traceLambda(LOG, "blah {}", paramStack::pop);
assertThat(paramStack.size()).isEqualTo(2);
traceLambda(LOG, "blah {} {}", paramStack::pop, paramStack::pop);
assertThat(paramStack.size()).isZero();
}
@Test
public void smokeWarnLambda() {
Log4j2ConfiguratorUtil.setLevel(LOG.getName(), Level.OFF);
traceLambda(
LOG,
"blah",
(Supplier<String>)
() -> {
throw new RuntimeException("should not evaluate");
});
Log4j2ConfiguratorUtil.setLevel(LOG.getName(), Level.WARN);
assertThat(paramStack.size()).isEqualTo(3);
warnLambda(LOG, "blah {}", paramStack::pop);
assertThat(paramStack.size()).isEqualTo(2);
warnLambda(LOG, "blah {} {}", paramStack::pop, paramStack::pop);
assertThat(paramStack.size()).isZero();
}
}