Skip to content

Commit

Permalink
feat: add logging configurability to the simulator (#378)
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <[email protected]>
  • Loading branch information
georgi-l95 authored Dec 5, 2024
1 parent 4cde6a4 commit ed30d1f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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()));
}
}
}
1 change: 1 addition & 0 deletions simulator/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions simulator/src/main/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ void start_millisPerBlockStreaming() throws InterruptedException, IOException, B
@Test
void start_millisPerSecond_streamingLagVerifyWarnLog()
throws InterruptedException, IOException, BlockSimulatorParsingException {
List<LogRecord> logRecords = captureLogs();

BlockStreamManager blockStreamManager = mock(BlockStreamManager.class);
BlockItem blockItem = BlockItem.newBuilder()
.setBlockHeader(BlockHeader.newBuilder().setNumber(1L).build())
Expand Down Expand Up @@ -205,6 +203,7 @@ void start_millisPerSecond_streamingLagVerifyWarnLog()

BlockStreamSimulatorApp blockStreamSimulator =
new BlockStreamSimulatorApp(configuration, blockStreamManager, publishStreamGrpcClient, metricsService);
List<LogRecord> logRecords = captureLogs();

blockStreamSimulator.start();
assertTrue(blockStreamSimulator.isRunning());
Expand Down

0 comments on commit ed30d1f

Please sign in to comment.