Update SLF4J version (#4587)

* Bump SLF4J version and replace helper for lambdas

Signed-off-by: Diego López León <dieguitoll@gmail.com>

* Remove unnecessary toString calls

Signed-off-by: Diego López León <dieguitoll@gmail.com>

* Replace unnecessaryy lambdas for constant references

Signed-off-by: Diego López León <dieguitoll@gmail.com>

---------

Signed-off-by: Diego López León <dieguitoll@gmail.com>
This commit is contained in:
Diego López León
2023-02-28 08:10:09 -03:00
committed by GitHub
parent b850aa7b05
commit 66c757dfa3
59 changed files with 855 additions and 902 deletions

View File

@@ -34,7 +34,7 @@ dependencies {
implementation 'com.google.guava:guava'
implementation 'org.apache.commons:commons-lang3'
implementation 'org.apache.logging.log4j:log4j-core'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl'
implementation 'org.apache.logging.log4j:log4j-slf4j2-impl'
implementation 'org.xerial.snappy:snappy-java'
testImplementation 'junit:junit'

View File

@@ -1,85 +0,0 @@
/*
* 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 {
private Slf4jLambdaHelper() {}
/**
* Warn lambda.
*
* @param log the log
* @param message the message
* @param params the params
*/
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());
}
}
/**
* Info lambda.
*
* @param log the log
* @param message the message
* @param params the params
*/
public static void infoLambda(
final Logger log, final String message, final Supplier<?>... params) {
if (log.isInfoEnabled()) {
log.info(message, Arrays.stream(params).map(Supplier::get).toArray());
}
}
/**
* Debug lambda.
*
* @param log the log
* @param message the message
* @param params the params
*/
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());
}
}
/**
* Trace lambda.
*
* @param log the log
* @param message the message
* @param params the params
*/
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());
}
}
}

View File

@@ -1,94 +0,0 @@
/*
* 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();
}
}