14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- package org .apache .servicemix .logging ;
17
+ package org .apache .servicemix .logging . jms ;
18
18
19
19
import org .ops4j .pax .logging .spi .PaxAppender ;
20
20
import org .ops4j .pax .logging .spi .PaxLoggingEvent ;
21
21
import org .slf4j .Logger ;
22
22
import org .slf4j .LoggerFactory ;
23
23
24
24
import javax .jms .*;
25
- import java .text .SimpleDateFormat ;
26
- import java .util .Date ;
27
25
28
26
public class JMSAppender implements PaxAppender {
29
27
30
28
private static final transient Logger LOG = LoggerFactory .getLogger (JMSAppender .class );
31
29
30
+ private static final String DEFAULT_EVENT_FORMAT = "default" ;
31
+ private static final String LOGSTASH_EVENT_FORMAT = "logstash" ;
32
+
33
+
32
34
private ConnectionFactory jmsConnectionFactory ;
33
35
private Connection connection ;
34
36
private Session session ;
35
37
private MessageProducer publisher ;
36
38
private Topic topic ;
37
39
private String destinationName ;
38
40
39
- private SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ" );
41
+ private LoggingEventFormat format = new DefaultLoggingEventFormat ();
42
+
43
+
40
44
41
45
public void init () {
42
46
/*
43
47
* Create connection. Create session from connection; false means
44
48
* session is not transacted.
45
- * Finally, close connection.
46
49
*/
47
50
try {
48
51
connection = jmsConnectionFactory .createConnection ();
@@ -70,56 +73,14 @@ public void close() {
70
73
}
71
74
72
75
public void doAppend (PaxLoggingEvent paxLoggingEvent ) {
73
-
74
76
try {
75
- StringBuilder writer = new StringBuilder ();
76
-
77
- writer .append ("Error" );
78
- writer .append (",\n \" timestamp\" : " + formatDate (paxLoggingEvent .getTimeStamp ()));
79
- writer .append (",\n \" level\" : " + paxLoggingEvent .getLevel ().toString ());
80
- writer .append (",\n \" logger\" : " + paxLoggingEvent .getLoggerName ());
81
- writer .append (",\n \" thread\" : " + paxLoggingEvent .getThreadName ());
82
- writer .append (",\n \" message\" : " + paxLoggingEvent .getMessage ());
83
-
84
- String [] throwable = paxLoggingEvent .getThrowableStrRep ();
85
- if (throwable != null ) {
86
- writer .append (",\n \" exception\" : [" );
87
- for (int i = 0 ; i < throwable .length ; i ++) {
88
- if (i != 0 )
89
- writer .append (", " + throwable [i ]);
90
- }
91
- writer .append ("]" );
92
- }
93
-
94
- writer .append (",\n \" properties\" : { " );
95
- boolean first = true ;
96
- for (Object key : paxLoggingEvent .getProperties ().keySet ()) {
97
- if (first ) {
98
- first = false ;
99
- } else {
100
- writer .append (", " );
101
- }
102
- writer .append ("key : " + key .toString ());
103
- writer .append (": " + paxLoggingEvent .getProperties ().get (key ).toString ());
104
- }
105
- writer .append (" }" );
106
- writer .append ("\n }" );
107
-
108
77
// Send message to the destination
109
78
TextMessage message = session .createTextMessage ();
110
- message .setText (writer .toString ());
79
+ message .setText (format .toString (paxLoggingEvent ));
111
80
publisher .send (message );
112
-
113
- // System.out.println(">> Message created : " + writer.toString());
114
-
115
- } catch (Exception e ) {
81
+ } catch (JMSException e ) {
116
82
e .printStackTrace ();
117
83
}
118
-
119
- }
120
-
121
- private String formatDate (long timestamp ) {
122
- return simpleDateFormat .format (new Date (timestamp ));
123
84
}
124
85
125
86
public void setJmsConnectionFactory (ConnectionFactory jmsConnectionFactory ) {
@@ -130,4 +91,11 @@ public void setDestinationName(String destinationName) {
130
91
this .destinationName = destinationName ;
131
92
}
132
93
94
+ public void setFormat (String name ) {
95
+ if (LOGSTASH_EVENT_FORMAT .equals (name )) {
96
+ format = new LogstashEventFormat ();
97
+ } else {
98
+ format = new DefaultLoggingEventFormat ();
99
+ }
100
+ }
133
101
}
0 commit comments