#1529 Adds logging suppression to Broadcaster to limit total logged error messages. (#1530)

Co-authored-by: Dennis Sheirer <dsheirer@github.com>
This commit is contained in:
Denny Sheirer
2023-04-24 04:36:02 -04:00
committed by GitHub
parent f877354a11
commit e8f563deeb
2 changed files with 160 additions and 8 deletions

View File

@@ -18,10 +18,10 @@
*/
package io.github.dsheirer.sample;
import io.github.dsheirer.log.LoggingSuppressor;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
@@ -29,11 +29,15 @@ import org.slf4j.LoggerFactory;
*/
public class Broadcaster<T> implements Listener<T>
{
private final static Logger mLog = LoggerFactory.getLogger(Broadcaster.class);
private static LoggingSuppressor sLoggingSuppressor;
private List<Listener<T>> mListeners = new CopyOnWriteArrayList<>();
/**
* Constructs an instance
*/
public Broadcaster()
{
sLoggingSuppressor = new LoggingSuppressor(LoggerFactory.getLogger(Broadcaster.class));
}
/**
@@ -117,16 +121,25 @@ public class Broadcaster<T> implements Listener<T>
*/
public void broadcast(T t)
{
try
for(Listener<T> listener: mListeners)
{
for(Listener<T> listener: mListeners)
try
{
listener.receive(t);
}
}
catch(Exception e)
{
mLog.error("Error while broadcasting [" + t.getClass() + "] to listeners");
catch(Exception e)
{
if(t != null)
{
sLoggingSuppressor.error(t.getClass().toGenericString(), 5,
"Error while broadcasting [" + t.getClass() + "] to listeners", e);
}
else
{
sLoggingSuppressor.error("null broadcast object", 5, "Can't broadcast null " +
"object to listener [" + listener.getClass() + "]", e);
}
}
}
}
}