From cf80428e0dc5b71bc4abc212ceaf50d6a65f3329 Mon Sep 17 00:00:00 2001 From: Mikko Kortelainen Date: Thu, 23 Jan 2025 10:27:04 +0200 Subject: [PATCH] add logging (#58) * add logging * add logging --- .../com/teragrep/aer_02/DefaultOutput.java | 26 +++++++++++++---- .../ManagedRelpConnectionWithMetrics.java | 16 +++++++---- ...nagedRelpConnectionWithMetricsFactory.java | 23 +++++++++++---- .../com/teragrep/aer_02/SyslogBridge.java | 8 ++++++ .../teragrep/aer_02/DefaultOutputTest.java | 28 +++++++++++++++++-- .../aer_02/EventDataConsumerTlsTest.java | 2 ++ 6 files changed, 84 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/teragrep/aer_02/DefaultOutput.java b/src/main/java/com/teragrep/aer_02/DefaultOutput.java index adc9e9c..99e8182 100644 --- a/src/main/java/com/teragrep/aer_02/DefaultOutput.java +++ b/src/main/java/com/teragrep/aer_02/DefaultOutput.java @@ -51,30 +51,31 @@ import com.teragrep.rlp_01.client.*; import com.teragrep.rlp_01.pool.Pool; import com.teragrep.rlp_01.pool.UnboundPool; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.logging.Logger; /** * Implementation of an shareable output. Required to be thread-safe. */ final class DefaultOutput implements Output { - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultOutput.class); - private final Pool relpConnectionPool; private final String relpAddress; private final int relpPort; + private final Logger logger; DefaultOutput( + Logger logger, String name, RelpConnectionConfig relpConnectionConfig, MetricRegistry metricRegistry, SSLContextSupplier sslContextSupplier ) { this( + logger, relpConnectionConfig, new UnboundPool<>( new ManagedRelpConnectionWithMetricsFactory( + logger, relpConnectionConfig.asRelpConfig(), name, metricRegistry, @@ -86,11 +87,18 @@ final class DefaultOutput implements Output { ); } - DefaultOutput(String name, RelpConnectionConfig relpConnectionConfig, MetricRegistry metricRegistry) { + DefaultOutput( + Logger logger, + String name, + RelpConnectionConfig relpConnectionConfig, + MetricRegistry metricRegistry + ) { this( + logger, relpConnectionConfig, new UnboundPool<>( new ManagedRelpConnectionWithMetricsFactory( + logger, relpConnectionConfig.asRelpConfig(), name, metricRegistry, @@ -101,11 +109,17 @@ final class DefaultOutput implements Output { ); } - DefaultOutput(RelpConnectionConfig relpConnectionConfig, Pool relpConnectionPool) { + DefaultOutput( + Logger logger, + RelpConnectionConfig relpConnectionConfig, + Pool relpConnectionPool + ) { this.relpAddress = relpConnectionConfig.relpAddress(); this.relpPort = relpConnectionConfig.relpPort(); this.relpConnectionPool = relpConnectionPool; + this.logger = logger; + logger.info("DefaultOutput constructor done"); } @Override diff --git a/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetrics.java b/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetrics.java index bf973e8..dc5868b 100644 --- a/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetrics.java +++ b/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetrics.java @@ -52,11 +52,13 @@ import java.io.IOException; import java.util.concurrent.TimeoutException; +import java.util.logging.Logger; import static com.codahale.metrics.MetricRegistry.name; public class ManagedRelpConnectionWithMetrics implements IManagedRelpConnection { + private final Logger logger; private final IRelpConnection relpConnection; private boolean hasConnected; @@ -70,11 +72,13 @@ public class ManagedRelpConnectionWithMetrics implements IManagedRelpConnection private final Timer connectLatency; public ManagedRelpConnectionWithMetrics( + Logger logger, IRelpConnection relpConnection, String name, MetricRegistry metricRegistry ) { this( + logger, relpConnection, name, metricRegistry, @@ -84,12 +88,14 @@ public ManagedRelpConnectionWithMetrics( } public ManagedRelpConnectionWithMetrics( + Logger logger, IRelpConnection relpConnection, String name, MetricRegistry metricRegistry, Reservoir sendReservoir, Reservoir connectReservoir ) { + this.logger = logger; this.relpConnection = relpConnection; this.records = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "records")); @@ -131,8 +137,8 @@ public void connect() { connects.inc(); } catch (IOException | TimeoutException e) { - System.err - .println( + logger + .warning( "Failed to connect to relp server <[" + relpConnection.relpConfig().relpTarget + "]>:<[" + relpConnection.relpConfig().relpPort + "]>: <" + e.getMessage() + ">" ); @@ -142,7 +148,7 @@ public void connect() { retriedConnects.inc(); } catch (InterruptedException exception) { - System.err.println("Reconnection timer interrupted, reconnecting now"); + logger.warning("Reconnection timer interrupted, reconnecting now"); } } } @@ -177,7 +183,7 @@ public void ensureSent(byte[] bytes) { bytesCnt.inc(bytes.length); } catch (IllegalStateException | IOException | TimeoutException e) { - System.err.println("Exception <" + e.getMessage() + "> while sending relpBatch. Will retry"); + logger.warning("Exception <" + e.getMessage() + "> while sending relpBatch. Will retry"); } if (!relpBatch.verifyTransactionAll()) { relpBatch.retryAllFailed(); @@ -203,7 +209,7 @@ public void close() { this.relpConnection.disconnect(); } catch (IllegalStateException | IOException | TimeoutException e) { - System.err.println("Forcefully closing connection due to exception <" + e.getMessage() + ">"); + logger.warning("Forcefully closing connection due to exception <" + e.getMessage() + ">"); } finally { tearDown(); diff --git a/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetricsFactory.java b/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetricsFactory.java index 15f304a..753f9cd 100644 --- a/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetricsFactory.java +++ b/src/main/java/com/teragrep/aer_02/ManagedRelpConnectionWithMetricsFactory.java @@ -48,6 +48,7 @@ import com.codahale.metrics.MetricRegistry; import com.teragrep.rlp_01.RelpConnection; import com.teragrep.rlp_01.client.*; +import java.util.logging.Logger; import java.util.function.Supplier; @@ -58,36 +59,46 @@ public class ManagedRelpConnectionWithMetricsFactory implements Supplier pool = new UnboundPool<>( () -> new ManagedRelpConnectionWithMetrics( + Logger.getAnonymousLogger(), new RelpConnectionWithConfig( new RelpConnectionFake(), new RelpConnectionConfig(new PropertySource()).asRelpConfig() @@ -100,7 +102,13 @@ public void testSendLatencyMetricIsCapped() { // Should only keep information on new ManagedRelpConnectionStub() ); - try (DefaultOutput output = new DefaultOutput(new RelpConnectionConfig(new PropertySource()), pool)) { + try ( + DefaultOutput output = new DefaultOutput( + Logger.getAnonymousLogger(), + new RelpConnectionConfig(new PropertySource()), + pool + ) + ) { for (int i = 0; i < measurementLimit + 100; i++) { // send more messages than the limit is output.accept(syslogMessage.toRfc5424SyslogMessage().getBytes(StandardCharsets.UTF_8)); @@ -131,12 +139,19 @@ public void testConnectionLatencyMetricIsCapped() { // Should take information o UnboundPool pool = new UnboundPool<>( () -> new ManagedRelpConnectionWithMetrics( + Logger.getAnonymousLogger(), new RelpConnectionWithConfig(new ConnectionlessRelpConnectionFake(reconnections), new RelpConnectionConfig(new PropertySource()).asRelpConfig()), "defaultOutput", metricRegistry, sendReservoir, connectReservoir ), new ManagedRelpConnectionStub() ); - try (DefaultOutput output = new DefaultOutput(new RelpConnectionConfig(new PropertySource()), pool)) { + try ( + DefaultOutput output = new DefaultOutput( + Logger.getAnonymousLogger(), + new RelpConnectionConfig(new PropertySource()), + pool + ) + ) { output.accept(syslogMessage.toRfc5424SyslogMessage().getBytes(StandardCharsets.UTF_8)); } @@ -161,12 +176,19 @@ public void testConnectionLatencyMetricWithException() { // should not update va MetricRegistry metricRegistry = new MetricRegistry(); UnboundPool pool = new UnboundPool<>( () -> new ManagedRelpConnectionWithMetrics( + Logger.getAnonymousLogger(), new RelpConnectionWithConfig(new ThrowingRelpConnectionFake(reconnections), new RelpConnectionConfig(new PropertySource()).asRelpConfig()), "defaultOutput", metricRegistry ), new ManagedRelpConnectionStub() ); - try (DefaultOutput output = new DefaultOutput(new RelpConnectionConfig(new PropertySource()), pool)) { + try ( + DefaultOutput output = new DefaultOutput( + Logger.getAnonymousLogger(), + new RelpConnectionConfig(new PropertySource()), + pool + ) + ) { output.accept(syslogMessage.toRfc5424SyslogMessage().getBytes(StandardCharsets.UTF_8)); } diff --git a/src/test/java/com/teragrep/aer_02/EventDataConsumerTlsTest.java b/src/test/java/com/teragrep/aer_02/EventDataConsumerTlsTest.java index 84702e3..7602825 100644 --- a/src/test/java/com/teragrep/aer_02/EventDataConsumerTlsTest.java +++ b/src/test/java/com/teragrep/aer_02/EventDataConsumerTlsTest.java @@ -86,6 +86,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; +import java.util.logging.Logger; public class EventDataConsumerTlsTest { @@ -176,6 +177,7 @@ public boolean isStub() { Sourceable configSource = new EnvironmentSource(); DefaultOutput defaultOutput = new DefaultOutput( + Logger.getAnonymousLogger(), "defaultOutput", new RelpConnectionConfig(configSource), metricRegistry,