Skip to content

Commit 7585b25

Browse files
Added filtering support to the AMQP consumer agent. (Updated logback-consumer.xml accordingly to exemplify.)
1 parent a6bbfd4 commit 7585b25

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

logback-amqp-consumer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
<artifactId>amqp-client</artifactId>
2929
<version>2.2.0</version>
3030
</dependency>
31+
<dependency>
32+
<groupId>janino</groupId>
33+
<artifactId>janino</artifactId>
34+
<version>2.5.10</version>
35+
</dependency>
3136
</dependencies>
3237
<build>
3338
<plugins>

logback-amqp-consumer/src/main/java/ch/qos/logback/amqp/AmqpConsumerAgent.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.concurrent.LinkedBlockingQueue;
66
import java.util.concurrent.TimeUnit;
77

8+
import ch.qos.logback.core.spi.FilterReply;
9+
810
import ch.qos.logback.amqp.tools.Callbacks;
911
import ch.qos.logback.amqp.tools.DefaultBinarySerializer;
1012
import ch.qos.logback.amqp.tools.DefaultContextAwareCallbacks;
@@ -13,6 +15,7 @@
1315
import ch.qos.logback.classic.Logger;
1416
import ch.qos.logback.classic.spi.ILoggingEvent;
1517
import ch.qos.logback.core.Context;
18+
import ch.qos.logback.core.filter.Filter;
1619
import ch.qos.logback.core.spi.ContextAwareBase;
1720
import ch.qos.logback.core.spi.LifeCycle;
1821
import org.slf4j.LoggerFactory;
@@ -32,6 +35,7 @@ public AmqpConsumerAgent ()
3235
this.queue = AmqpConsumerAgent.defaultQueue;
3336
this.routingKey = AmqpConsumerAgent.defaultRoutingKey;
3437
this.serializer = new DefaultBinarySerializer ();
38+
this.filter = null;
3539
this.thread = null;
3640
this.shutdownHook = null;
3741
this.shouldStop = true;
@@ -44,6 +48,11 @@ public final String getExchange ()
4448
return (this.exchange);
4549
}
4650

51+
public final Filter<ILoggingEvent> getFilter ()
52+
{
53+
return (this.filter);
54+
}
55+
4756
public final String getHost ()
4857
{
4958
return (this.host);
@@ -124,6 +133,15 @@ public final void setExchange (final String exchange)
124133
}
125134
}
126135

136+
public final void setFilter (final Filter<ILoggingEvent> filter)
137+
{
138+
synchronized (this) {
139+
if (this.thread != null)
140+
throw (new IllegalStateException ("amqp appender is already started"));
141+
this.filter = filter;
142+
}
143+
}
144+
127145
public final void setHost (final String host)
128146
{
129147
synchronized (this) {
@@ -236,8 +254,8 @@ public final void run ()
236254
this.thread.start ();
237255
this.consumer =
238256
new AmqpConsumer (
239-
this.host, this.port, this.virtualHost, this.username, this.password,
240-
this.exchange, this.queue, this.routingKey, this.callbacks, this.buffer);
257+
this.host, this.port, this.virtualHost, this.username, this.password, this.exchange, this.queue,
258+
this.routingKey, this.callbacks, this.buffer);
241259
this.consumer.start ();
242260
this.isStarted = true;
243261
this.postStart ();
@@ -299,6 +317,9 @@ private final void loop ()
299317
continue;
300318
}
301319
try {
320+
if (this.filter != null)
321+
if (this.filter.decide (event) == FilterReply.DENY)
322+
continue;
302323
this.process (event);
303324
} catch (final Throwable exception) {
304325
this.addError (
@@ -311,6 +332,7 @@ private final void loop ()
311332
private final LinkedBlockingQueue<AmqpMessage> buffer;
312333
private AmqpConsumer consumer;
313334
private String exchange;
335+
private Filter<ILoggingEvent> filter;
314336
private String host;
315337
private boolean isStarted;
316338
private String password;

logback-amqp-consumer/src/test/config/logback-consumer.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@
1313
<exchange>logback</exchange>
1414
<queue></queue>
1515
<routingKey>#</routingKey>
16+
<!--
17+
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
18+
<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
19+
<expression>
20+
final String applicationFilter = (String) System.getProperty ("amqp.consumer.agent.application_filter");
21+
if ((applicationFilter == null) || applicationFilter.isEmpty ())
22+
return (true);
23+
final String applicationValue = (mdc != null) ? (String) mdc.get("application") : null;
24+
if ((applicationValue == null) || applicationValue.isEmpty ())
25+
return (true);
26+
return (applicationFilter.equals (applicationValue));
27+
</expression>
28+
</evaluator>
29+
<onMismatch>DENY</onMismatch>
30+
<onMatch>NEUTRAL</onMatch>
31+
</filter>
32+
-->
1633
</amqpConsumerAgent>
1734

1835
<appender name="sifter" class="ch.qos.logback.classic.sift.SiftingAppender">

0 commit comments

Comments
 (0)