-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnhancedJMSAppender.java
61 lines (52 loc) · 1.98 KB
/
EnhancedJMSAppender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.danveloper.log4j.jms;
import org.apache.activemq.ActiveMQConnection;
import org.apache.log4j.net.JMSAppender;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* User: ddcdwoods
* Date: 3/19/13
*/
public class EnhancedJMSAppender extends JMSAppender {
private String appName;
/**
* Method enriches the headers so that JMS consumers know who you are
*/
@Override
public void append(LoggingEvent event) {
if(!checkEntryConditions()) {
return;
}
try {
ActiveMQConnection activeMqConnection = (ActiveMQConnection)getTopicConnection();
if (!activeMqConnection.isStarted()) {
activeMqConnection.start();
}
ObjectMessage msg = getTopicSession().createObjectMessage();
if (getLocationInfo()) {
event.getLocationInformation();
}
msg.setStringProperty("application.name", appName);
msg.setStringProperty("hostname", InetAddress.getLocalHost().getHostName());
msg.setStringProperty("ip-address", InetAddress.getLocalHost().getHostAddress());
msg.setObject(event);
getTopicPublisher().publish(msg);
} catch(JMSException e) {
errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e,
ErrorCode.GENERIC_FAILURE);
} catch(RuntimeException e) {
errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e,
ErrorCode.GENERIC_FAILURE);
} catch (UnknownHostException e) {
errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e,
ErrorCode.GENERIC_FAILURE);
}
}
public void setAppName(String appName) {
this.appName = appName;
}
}