Skip to content

Commit

Permalink
Replace Log4j2 with logback
Browse files Browse the repository at this point in the history
Signed-off-by: Diego López León <dieguitoll@gmail.com>
  • Loading branch information
diega committed Apr 11, 2022
1 parent e9e7d7e commit ed81df8
Show file tree
Hide file tree
Showing 31 changed files with 211 additions and 405 deletions.
2 changes: 1 addition & 1 deletion acceptance-tests/tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
testImplementation project(':testutil')
testImplementation project(':util')

testImplementation 'ch.qos.logback:logback-classic'
testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone'
testImplementation 'commons-io:commons-io'
testImplementation 'io.grpc:grpc-core'
Expand All @@ -67,7 +68,6 @@ dependencies {
testImplementation 'io.vertx:vertx-core'
testImplementation 'junit:junit'
testImplementation 'org.apache.commons:commons-compress'
testImplementation 'org.apache.logging.log4j:log4j-core'
testImplementation 'org.apache.tuweni:tuweni-crypto'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.awaitility:awaitility'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyAcceptanceTestBase;
import org.hyperledger.besu.tests.acceptance.dsl.privacy.PrivacyNode;
import org.hyperledger.besu.tests.web3j.generated.EventEmitter;
import org.hyperledger.besu.util.Log4j2ConfiguratorUtil;
import org.hyperledger.besu.util.LogbackConfiguratorUtil;
import org.hyperledger.enclave.testutil.EnclaveType;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Optional;

import org.apache.logging.log4j.Level;
import ch.qos.logback.classic.Level;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -87,7 +87,7 @@ public PrivacyGroupAcceptanceTest(final EnclaveType enclaveType) throws IOExcept

@Test
public void nodeCanCreatePrivacyGroup() {
Log4j2ConfiguratorUtil.setLevel("", Level.DEBUG);
LogbackConfiguratorUtil.setLevel("", Level.DEBUG);
final String privacyGroupId =
alice.execute(
privacyTransactions.createPrivacyGroup(
Expand Down
6 changes: 4 additions & 2 deletions besu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ dependencies {
implementation project(':services:kvstore')
implementation project(':util')

implementation 'ch.qos.logback:logback-classic'
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8'
implementation 'com.google.guava:guava'
implementation 'com.graphql-java:graphql-java'
implementation 'info.picocli:picocli'
implementation 'io.vertx:vertx-core'
implementation 'io.vertx:vertx-web'
implementation 'org.apache.logging.log4j:log4j-core'
implementation 'org.apache.tuweni:tuweni-bytes'
implementation 'org.apache.tuweni:tuweni-config'
implementation 'org.apache.tuweni:tuweni-toml'
Expand All @@ -76,9 +76,11 @@ dependencies {
implementation 'org.xerial.snappy:snappy-java'
implementation 'net.consensys.services:quorum-mainnet-launcher'

runtimeOnly 'org.apache.logging.log4j:log4j-jul'
runtimeOnly 'com.splunk.logging:splunk-library-javalogging'
runtimeOnly 'org.codehaus.janino:janino'
runtimeOnly 'org.codehaus.janino:commons-compiler'
runtimeOnly 'org.fusesource.jansi:jansi' // for color logging in windows
runtimeOnly 'org.slf4j:jul-to-slf4j'

testImplementation project(path: ':ethereum:core', configuration: 'testSupportArtifacts')
testImplementation project(path: ':crypto', configuration: 'testSupportArtifacts')
Expand Down
10 changes: 3 additions & 7 deletions besu/src/main/java/org/hyperledger/besu/Besu.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
import org.hyperledger.besu.chainimport.JsonBlockImporter;
import org.hyperledger.besu.chainimport.RlpBlockImporter;
import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.logging.BesuLoggingConfigurationFactory;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.services.BesuPluginContextImpl;

import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Log4J2LoggerFactory;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine.RunLast;
Expand Down Expand Up @@ -54,14 +53,11 @@ public static void main(final String... args) {
}

private static Logger setupLogging() {
InternalLoggerFactory.setDefaultFactory(Log4J2LoggerFactory.INSTANCE);
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
try {
System.setProperty(
"vertx.logger-delegate-factory-class-name",
"io.vertx.core.logging.Log4j2LogDelegateFactory");
System.setProperty(
"log4j.configurationFactory", BesuLoggingConfigurationFactory.class.getName());
System.setProperty("log4j.skipJansi", String.valueOf(false));
"io.vertx.core.logging.SLF4JLogDelegateFactory");
} catch (SecurityException e) {
System.out.println(
"Could not set logging system property as the security manager prevented it:"
Expand Down
31 changes: 14 additions & 17 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.hyperledger.besu.cli.custom.JsonRPCAllowlistHostsProperty;
import org.hyperledger.besu.cli.custom.RpcAuthFileValidator;
import org.hyperledger.besu.cli.error.BesuExceptionHandler;
import org.hyperledger.besu.cli.options.stable.ColorEnabledOption;
import org.hyperledger.besu.cli.options.stable.DataStorageOptions;
import org.hyperledger.besu.cli.options.stable.EthstatsOptions;
import org.hyperledger.besu.cli.options.stable.LoggingLevelOption;
Expand Down Expand Up @@ -178,7 +179,7 @@
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
import org.hyperledger.besu.services.StorageServiceImpl;
import org.hyperledger.besu.services.kvstore.InMemoryStoragePlugin;
import org.hyperledger.besu.util.Log4j2ConfiguratorUtil;
import org.hyperledger.besu.util.LogbackConfiguratorUtil;
import org.hyperledger.besu.util.NetworkUtility;
import org.hyperledger.besu.util.PermissioningConfigurationValidator;
import org.hyperledger.besu.util.number.Fraction;
Expand Down Expand Up @@ -217,6 +218,8 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

import ch.qos.logback.classic.Level;
import ch.qos.logback.core.joran.spi.JoranException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Suppliers;
Expand All @@ -231,7 +234,6 @@
import net.consensys.quorum.mainnet.launcher.config.ImmutableLauncherConfig;
import net.consensys.quorum.mainnet.launcher.exception.LauncherException;
import net.consensys.quorum.mainnet.launcher.util.ParseArgsHelper;
import org.apache.logging.log4j.Level;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.slf4j.Logger;
Expand Down Expand Up @@ -291,6 +293,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
private final NodePrivateKeyFileOption nodePrivateKeyFileOption =
NodePrivateKeyFileOption.create();
private final LoggingLevelOption loggingLevelOption = LoggingLevelOption.create();
private final ColorEnabledOption colorEnabledOption = ColorEnabledOption.create();

private final RunnerBuilder runnerBuilder;
private final BesuController.Builder controllerBuilderFactory;
Expand Down Expand Up @@ -978,13 +981,6 @@ static class MetricsOptionGroup {
"Deprecated in favor of --host-allowlist. Comma separated list of hostnames to allow for RPC access, or * to accept any host (default: ${DEFAULT-VALUE})")
private final JsonRPCAllowlistHostsProperty hostsWhitelist = new JsonRPCAllowlistHostsProperty();

@SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"})
@Option(
names = {"--color-enabled"},
description =
"Force color output to be enabled/disabled (default: colorized only if printing to console)")
private static Boolean colorEnabled = null;

@Option(
names = {"--reorg-logging-threshold"},
description =
Expand Down Expand Up @@ -1465,6 +1461,7 @@ private void handleStableOptions() {
commandLine.addMixin("Private key file", nodePrivateKeyFileOption);
commandLine.addMixin("Logging level", loggingLevelOption);
commandLine.addMixin("Data Storage Options", dataStorageOptions);
commandLine.addMixin("Color enabled", colorEnabledOption);
}

private void handleUnstableOptions() {
Expand Down Expand Up @@ -1645,21 +1642,21 @@ private void validatePluginOptions() {

public void configureLogging(final boolean announce) {
// To change the configuration if color was enabled/disabled
Log4j2ConfiguratorUtil.reconfigure();
try {
LogbackConfiguratorUtil.reconfigure();
} catch (JoranException e) {
System.err.println("Unable to reconfigure logback: " + e.getMessage());
}
// set log level per CLI flags
final Level logLevel = loggingLevelOption.getLogLevel();
if (logLevel != null) {
if (announce) {
System.out.println("Setting logging level to " + logLevel.name());
System.out.println("Setting logging level to " + logLevel);
}
Log4j2ConfiguratorUtil.setAllLevels("", logLevel);
LogbackConfiguratorUtil.setAllLevels("", logLevel);
}
}

public static Optional<Boolean> getColorEnabled() {
return Optional.ofNullable(colorEnabled);
}

private void configureNativeLibs() {
if (unstableNativeLibraryOptions.getNativeAltbn128()
&& AbstractAltBnPrecompiledContract.isNative()) {
Expand Down Expand Up @@ -2824,7 +2821,7 @@ private void addShutdownHook(final Runner runner) {
try {
besuPluginContext.stopPlugins();
runner.close();
Log4j2ConfiguratorUtil.shutdown();
LogbackConfiguratorUtil.shutdown();
} catch (final Exception e) {
logger.error("Failed to stop Besu");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.List;
import java.util.function.Supplier;

import org.apache.logging.log4j.Level;
import ch.qos.logback.classic.Level;
import picocli.CommandLine;

public class BesuExceptionHandler
Expand All @@ -34,7 +34,7 @@ public BesuExceptionHandler(final Supplier<Level> levelSupplier) {
public List<Object> handleParseException(
final CommandLine.ParameterException ex, final String[] args) {
final Level logLevel = levelSupplier.get();
if (logLevel != null && Level.DEBUG.isMoreSpecificThan(logLevel)) {
if (logLevel != null && Level.DEBUG.isGreaterOrEqual(logLevel)) {
ex.printStackTrace(err());
} else {
err().println(ex.getMessage());
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit ed81df8

Please sign in to comment.