From d52931af720f655ca31fdbe8a760ccaf08627cef Mon Sep 17 00:00:00 2001 From: Daniel Schroeter Date: Wed, 10 Aug 2016 08:38:27 +0200 Subject: [PATCH] if the fluent logger is used as a logger for slf4j and it fails to send messages it will choke on its own failure messages (cannot send message) ->this change breaks this circle by only logging the failure to send messages once per minute --- .../fluentd/logger/sender/RawSocketSender.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/fluentd/logger/sender/RawSocketSender.java b/src/main/java/org/fluentd/logger/sender/RawSocketSender.java index c842b9f..1889464 100644 --- a/src/main/java/org/fluentd/logger/sender/RawSocketSender.java +++ b/src/main/java/org/fluentd/logger/sender/RawSocketSender.java @@ -54,6 +54,8 @@ public class RawSocketSender implements Sender { private ErrorHandler errorHandler = DEFAULT_ERROR_HANDLER; + private long lastCannotSendLogTimestamp = 0; + public RawSocketSender() { this("localhost", 24224); } @@ -130,9 +132,7 @@ public boolean emit(String tag, long timestamp, Map data) { } protected boolean emit(Event event) { - if (LOG.isTraceEnabled()) { - LOG.trace(String.format("Created %s", new Object[]{event})); - } + LOG.trace("Created {}", event); byte[] bytes = null; try { @@ -162,7 +162,7 @@ private synchronized boolean send(byte[] bytes) { // buffering if (pendings.position() + bytes.length > pendings.capacity()) { if (!flushBuffer()) { - LOG.error("Cannot send logs to " + server.toString()); + logCannotSendMaxOncePerMinute(); return false; } } @@ -179,6 +179,14 @@ private synchronized boolean send(byte[] bytes) { return true; } + private void logCannotSendMaxOncePerMinute() { + // to not choke on our own log message + if(System.currentTimeMillis() + 60 * 1000 > lastCannotSendLogTimestamp) { + LOG.error("Cannot send logs to " + server.toString()); + lastCannotSendLogTimestamp = System.currentTimeMillis(); + } + } + @Override public synchronized void flush() { try {