From ed30d1f7bc1f689dfb12f381231da0e024d017bc Mon Sep 17 00:00:00 2001 From: Georgi Lazarov Date: Thu, 5 Dec 2024 19:34:51 +0200 Subject: [PATCH] feat: add logging configurability to the simulator (#378) Signed-off-by: georgi-l95 --- .../simulator/BlockStreamSimulatorApp.java | 29 +++++++++++++++++++ simulator/src/main/java/module-info.java | 1 + .../src/main/resources/logging.properties | 10 +++++++ .../simulator/BlockStreamSimulatorTest.java | 3 +- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 simulator/src/main/resources/logging.properties diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java index 16dbc7767..7cdb2205f 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java @@ -16,6 +16,8 @@ package com.hedera.block.simulator; +import static com.hedera.block.common.constants.StringsConstants.LOGGING_PROPERTIES; +import static java.lang.System.Logger.Level.ERROR; import static java.lang.System.Logger.Level.INFO; import static java.util.Objects.requireNonNull; @@ -33,7 +35,12 @@ import com.swirlds.config.api.Configuration; import edu.umd.cs.findbugs.annotations.NonNull; import java.io.IOException; +import java.io.InputStream; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.ConsoleHandler; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; import javax.inject.Inject; /** BlockStream Simulator App */ @@ -65,6 +72,8 @@ public BlockStreamSimulatorApp( requireNonNull(blockStreamManager); this.metricsService = requireNonNull(metricsService); this.publishStreamGrpcClient = requireNonNull(publishStreamGrpcClient); + loadLoggingProperties(); + final BlockStreamConfig blockStreamConfig = requireNonNull(configuration.getConfigData(BlockStreamConfig.class)); @@ -128,4 +137,24 @@ public StreamStatus getStreamStatus() { .lastKnownPublisherStatuses(publishStreamGrpcClient.getLastKnownStatuses()) .build(); } + + private void loadLoggingProperties() { + final LogManager logManager = LogManager.getLogManager(); + try (InputStream is = BlockStreamSimulator.class.getClassLoader().getResourceAsStream(LOGGING_PROPERTIES)) { + logManager.readConfiguration(is); + } catch (IOException | NullPointerException e) { + logManager.reset(); + Logger rootLogger = logManager.getLogger(""); + ConsoleHandler consoleHandler = new ConsoleHandler(); + + consoleHandler.setLevel(Level.INFO); + rootLogger.setLevel(Level.INFO); + rootLogger.addHandler(consoleHandler); + + LOGGER.log( + ERROR, + "Loading Logging Configuration failed, continuing with default. Error is: %s" + .formatted(e.getMessage())); + } + } } diff --git a/simulator/src/main/java/module-info.java b/simulator/src/main/java/module-info.java index e7c4075eb..8d7b52990 100644 --- a/simulator/src/main/java/module-info.java +++ b/simulator/src/main/java/module-info.java @@ -23,6 +23,7 @@ requires dagger; requires io.grpc.stub; requires io.grpc; + requires java.logging; requires javax.inject; provides com.swirlds.config.api.ConfigurationExtension with diff --git a/simulator/src/main/resources/logging.properties b/simulator/src/main/resources/logging.properties new file mode 100644 index 000000000..1e0fcd8df --- /dev/null +++ b/simulator/src/main/resources/logging.properties @@ -0,0 +1,10 @@ +# Set the default logging level +.level=INFO + +# Console handler configuration +handlers = java.util.logging.ConsoleHandler +java.util.logging.ConsoleHandler.level = INFO +java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter + +# gRPC Logging Configuration +io.grpc.level = INFO diff --git a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java index bb217c89d..861614613 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java @@ -170,8 +170,6 @@ void start_millisPerBlockStreaming() throws InterruptedException, IOException, B @Test void start_millisPerSecond_streamingLagVerifyWarnLog() throws InterruptedException, IOException, BlockSimulatorParsingException { - List logRecords = captureLogs(); - BlockStreamManager blockStreamManager = mock(BlockStreamManager.class); BlockItem blockItem = BlockItem.newBuilder() .setBlockHeader(BlockHeader.newBuilder().setNumber(1L).build()) @@ -205,6 +203,7 @@ void start_millisPerSecond_streamingLagVerifyWarnLog() BlockStreamSimulatorApp blockStreamSimulator = new BlockStreamSimulatorApp(configuration, blockStreamManager, publishStreamGrpcClient, metricsService); + List logRecords = captureLogs(); blockStreamSimulator.start(); assertTrue(blockStreamSimulator.isRunning());