Skip to content

Commit 24f53ac

Browse files
Added event mutator.
1 parent a178001 commit 24f53ac

File tree

6 files changed

+400
-12
lines changed

6 files changed

+400
-12
lines changed

logback-amqp-appender/src/main/java/ch/qos/logback/amqp/AmqpAppender.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
package ch.qos.logback.amqp;
33

44

5-
import java.io.Serializable;
65
import java.util.concurrent.LinkedBlockingDeque;
76

87
import ch.qos.logback.amqp.tools.DefaultBinarySerializer;
98
import ch.qos.logback.amqp.tools.DefaultContextAwareCallbacks;
9+
import ch.qos.logback.amqp.tools.DefaultMutator;
10+
import ch.qos.logback.amqp.tools.Mutator;
11+
import ch.qos.logback.amqp.tools.PubLoggingEventVO;
1012
import ch.qos.logback.amqp.tools.Serializer;
1113
import ch.qos.logback.classic.PatternLayout;
12-
import ch.qos.logback.classic.net.LoggingEventPreSerializationTransformer;
1314
import ch.qos.logback.classic.spi.ILoggingEvent;
1415
import ch.qos.logback.core.Context;
1516
import ch.qos.logback.core.UnsynchronizedAppenderBase;
16-
import ch.qos.logback.core.spi.PreSerializationTransformer;
1717

1818

1919
public final class AmqpAppender
@@ -22,16 +22,21 @@ public final class AmqpAppender
2222
public AmqpAppender ()
2323
{
2424
super ();
25-
this.preserializer = new LoggingEventPreSerializationTransformer ();
2625
this.serializer = new DefaultBinarySerializer ();
2726
this.buffer = new LinkedBlockingDeque<AmqpMessage> ();
2827
this.exchangeLayout = new PatternLayout ();
2928
this.routingKeyLayout = new PatternLayout ();
3029
this.exchangeLayout.setPattern (AmqpAppender.defaultExchangeKeyPattern);
3130
this.routingKeyLayout.setPattern (AmqpAppender.defaultRoutingKeyPattern);
31+
this.mutator = new DefaultMutator ();
3232
this.publisher = null;
3333
}
3434

35+
public final Mutator getMutator ()
36+
{
37+
return (this.mutator);
38+
}
39+
3540
public final boolean isDrained ()
3641
{
3742
return (this.buffer.isEmpty ());
@@ -66,6 +71,13 @@ public final void setHost (final String host)
6671
this.host = host;
6772
}
6873

74+
public final void setMutator (final Mutator mutator)
75+
{
76+
if (this.isStarted ())
77+
throw (new IllegalStateException ("amqp appender is already started"));
78+
this.mutator = mutator;
79+
}
80+
6981
public final void setPassword (final String password)
7082
{
7183
if (this.isStarted ())
@@ -127,19 +139,19 @@ public final void stop ()
127139
super.stop ();
128140
}
129141

130-
protected final void append (final ILoggingEvent event)
142+
protected final void append (final ILoggingEvent originalEvent)
131143
{
144+
final PubLoggingEventVO event = this.prepare (originalEvent);
132145
byte[] data;
133146
try {
134-
final Serializable object = this.preserializer.transform (event);
135-
data = this.serializer.serialize (object);
147+
data = this.serializer.serialize (event);
136148
} catch (final Throwable exception) {
137149
data = null;
138150
this.addError ("amqp appender encountered an error while serializing the event; ignoring!", exception);
139151
}
140152
if (data != null) {
141-
final String exchange = this.exchangeLayout.doLayout (event);
142-
final String routingKey = this.routingKeyLayout.doLayout (event);
153+
final String exchange = this.exchangeLayout.doLayout (originalEvent);
154+
final String routingKey = this.routingKeyLayout.doLayout (originalEvent);
143155
final AmqpMessage message =
144156
new AmqpMessage (
145157
exchange, routingKey, this.serializer.getContentType (), this.serializer.getContentEncoding (),
@@ -148,12 +160,20 @@ protected final void append (final ILoggingEvent event)
148160
}
149161
}
150162

163+
private final PubLoggingEventVO prepare (final ILoggingEvent originalEvent)
164+
{
165+
final PubLoggingEventVO newEvent = PubLoggingEventVO.build (originalEvent);
166+
if (this.mutator != null)
167+
this.mutator.mutate (newEvent);
168+
return (newEvent);
169+
}
170+
151171
private final LinkedBlockingDeque<AmqpMessage> buffer;
152172
private final PatternLayout exchangeLayout;
153173
private String host;
174+
private Mutator mutator;
154175
private String password;
155176
private Integer port;
156-
private final PreSerializationTransformer<ILoggingEvent> preserializer;
157177
private AmqpPublisher publisher;
158178
private final PatternLayout routingKeyLayout;
159179
private final Serializer serializer;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
package ch.qos.logback.amqp.tools;
3+
4+
5+
import java.net.InetAddress;
6+
import java.net.UnknownHostException;
7+
import java.util.HashMap;
8+
9+
10+
public class DefaultMutator
11+
implements
12+
Mutator
13+
{
14+
public DefaultMutator ()
15+
{
16+
this.sequence = 0;
17+
this.application = System.getProperty ("application");
18+
this.component = System.getProperty ("component");
19+
try {
20+
this.node = InetAddress.getLocalHost ().getHostName ();
21+
} catch (final UnknownHostException exception) {
22+
this.node = null;
23+
}
24+
}
25+
26+
public String getApplication ()
27+
{
28+
return (this.application);
29+
}
30+
31+
public String getComponent ()
32+
{
33+
return (this.component);
34+
}
35+
36+
public String getNode ()
37+
{
38+
return (this.node);
39+
}
40+
41+
public long getSequence ()
42+
{
43+
return (this.sequence);
44+
}
45+
46+
public void mutate (final PubLoggingEventVO event)
47+
{
48+
long sequence;
49+
synchronized (this) {
50+
sequence = this.sequence;
51+
this.sequence++;
52+
}
53+
if (event.mdcPropertyMap == null)
54+
event.mdcPropertyMap = new HashMap<String, String> (3);
55+
else
56+
event.mdcPropertyMap = new HashMap<String, String> (event.mdcPropertyMap);
57+
event.mdcPropertyMap.put (DefaultMutator.sequenceKey, Long.toString (sequence));
58+
event.mdcPropertyMap.put (DefaultMutator.applicationKey, this.application != null ? this.application : "unknown");
59+
event.mdcPropertyMap.put (DefaultMutator.componentKey, this.component != null ? this.component : "unknown");
60+
event.mdcPropertyMap.put (DefaultMutator.nodeKey, this.node != null ? this.node : "unknown");
61+
}
62+
63+
public void setApplication (final String application)
64+
{
65+
this.application = application;
66+
}
67+
68+
public void setComponent (final String component)
69+
{
70+
this.component = component;
71+
}
72+
73+
public void setNode (final String node)
74+
{
75+
this.node = node;
76+
}
77+
78+
protected String application;
79+
protected String component;
80+
protected String node;
81+
private long sequence;
82+
83+
public static final String applicationKey = "application";
84+
public static final String componentKey = "component";
85+
public static final String nodeKey = "node";
86+
public static final String sequenceKey = "sequence";
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
package ch.qos.logback.amqp.tools;
3+
4+
5+
public interface Mutator
6+
{
7+
public abstract void mutate (final PubLoggingEventVO event);
8+
}

0 commit comments

Comments
 (0)