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 16dbc776..7cdb2205 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 e7c4075e..8d7b5299 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 00000000..1e0fcd8d --- /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 bb217c89..86161461 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());