mirror of
https://github.com/vacp2p/linea-besu.git
synced 2026-01-08 20:47:59 -05:00
Reduce Log4J API Exposures (#5189)
Reduce the number of places that expose Log4J classes as a part of the interfaces for methods and classes. While Log4j remains the default we still need to be able to function when the Log4J jars are removed from the classpath. Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
*/
|
||||
package org.hyperledger.besu.util;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -26,7 +28,7 @@ import org.apache.logging.slf4j.Log4jLoggerFactory;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/** The Log4j2 configurator util. */
|
||||
public class Log4j2ConfiguratorUtil {
|
||||
class Log4j2ConfiguratorUtil {
|
||||
|
||||
private Log4j2ConfiguratorUtil() {}
|
||||
|
||||
@@ -36,18 +38,20 @@ public class Log4j2ConfiguratorUtil {
|
||||
* @param parentLogger the parent logger
|
||||
* @param level the level
|
||||
*/
|
||||
public static void setAllLevels(final String parentLogger, final Level level) {
|
||||
static void setAllLevels(final String parentLogger, final String level) {
|
||||
// 1) get logger config
|
||||
// 2) if exact match, use it, if not, create it.
|
||||
// 3) set level on logger config
|
||||
// 4) update child logger configs with level
|
||||
// 5) update loggers
|
||||
Level log4JLevel = Level.toLevel(level, null);
|
||||
requireNonNull(log4JLevel);
|
||||
final LoggerContext loggerContext = getLoggerContext();
|
||||
final Configuration config = loggerContext.getConfiguration();
|
||||
boolean set = setLevel(parentLogger, level, config);
|
||||
boolean set = setLevel(parentLogger, log4JLevel, config);
|
||||
for (final Map.Entry<String, LoggerConfig> entry : config.getLoggers().entrySet()) {
|
||||
if (entry.getKey().startsWith(parentLogger)) {
|
||||
set |= setLevel(entry.getValue(), level);
|
||||
set |= setLevel(entry.getValue(), log4JLevel);
|
||||
}
|
||||
}
|
||||
if (set) {
|
||||
@@ -60,8 +64,8 @@ public class Log4j2ConfiguratorUtil {
|
||||
*
|
||||
* @param loggerName the logger name
|
||||
*/
|
||||
public static void setLevelDebug(final String loggerName) {
|
||||
setLevel(loggerName, Level.DEBUG);
|
||||
static void setLevelDebug(final String loggerName) {
|
||||
setLevel(loggerName, "DEBUG");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,11 +74,13 @@ public class Log4j2ConfiguratorUtil {
|
||||
* @param loggerName the logger name
|
||||
* @param level the level
|
||||
*/
|
||||
public static void setLevel(final String loggerName, final Level level) {
|
||||
static void setLevel(final String loggerName, final String level) {
|
||||
Level log4jLevel = Level.toLevel(level, null);
|
||||
requireNonNull(log4jLevel);
|
||||
final LoggerContext loggerContext = getLoggerContext();
|
||||
if (Strings.isEmpty(loggerName)) {
|
||||
setRootLevel(loggerContext, level);
|
||||
} else if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
|
||||
setRootLevel(loggerContext, log4jLevel);
|
||||
} else if (setLevel(loggerName, log4jLevel, loggerContext.getConfiguration())) {
|
||||
loggerContext.updateLoggers();
|
||||
}
|
||||
}
|
||||
@@ -111,7 +117,7 @@ public class Log4j2ConfiguratorUtil {
|
||||
}
|
||||
|
||||
/** Reconfigure. */
|
||||
public static void reconfigure() {
|
||||
static void reconfigure() {
|
||||
getLoggerContext().reconfigure();
|
||||
}
|
||||
|
||||
@@ -122,7 +128,7 @@ public class Log4j2ConfiguratorUtil {
|
||||
}
|
||||
|
||||
/** Shutdown. */
|
||||
public static void shutdown() {
|
||||
static void shutdown() {
|
||||
getLoggerContext().terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.NoSuchElementException;
|
||||
|
||||
/** The library independent logger configurator util. */
|
||||
@SuppressWarnings("CatchAndPrintStackTrace")
|
||||
public interface LogConfigurator {
|
||||
|
||||
/**
|
||||
* Sets level to specified logger.
|
||||
*
|
||||
* @param parentLogger the logger name
|
||||
* @param level the level
|
||||
*/
|
||||
static void setLevel(final String parentLogger, final String level) {
|
||||
try {
|
||||
Log4j2ConfiguratorUtil.setAllLevels(parentLogger, level);
|
||||
} catch (NoClassDefFoundError | ClassCastException | NoSuchElementException e) {
|
||||
// This is expected when Log4j support is not in the classpath, so ignore
|
||||
}
|
||||
}
|
||||
|
||||
/** Reconfigure. */
|
||||
static void reconfigure() {
|
||||
try {
|
||||
Log4j2ConfiguratorUtil.reconfigure();
|
||||
} catch (NoClassDefFoundError | ClassCastException | NoSuchElementException e) {
|
||||
// This is expected when Log4j support is not in the classpath, so ignore
|
||||
}
|
||||
}
|
||||
|
||||
/** Shutdown. */
|
||||
static void shutdown() {
|
||||
try {
|
||||
Log4j2ConfiguratorUtil.shutdown();
|
||||
} catch (NoClassDefFoundError | ClassCastException | NoSuchElementException e) {
|
||||
// This is expected when Log4j support is not in the classpath, so ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user