Author: gertv
Date: Wed Oct 3 09:02:18 2012
New Revision: 1393356
URL: http://svn.apache.org/viewvc?rev=1393356&view=rev
Log:
SMX4-1256: Add Logstash event message format
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/DefaultLoggingEventFormat.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
- copied, changed from r1386914,
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/JMSAppender.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LoggingEventFormat.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LogstashEventFormat.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/LogstashEventFormatTest.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/resources/
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/resources/log4j.properties
Removed:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/JMSAppender.java
Modified:
servicemix/smx4/features/trunk/.gitignore
servicemix/smx4/features/trunk/osgi-logging/jms-appender/pom.xml
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
Modified: servicemix/smx4/features/trunk/.gitignore
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/.gitignore?rev=1393356&r1=1393355&r2=1393356&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/.gitignore (original)
+++ servicemix/smx4/features/trunk/.gitignore Wed Oct 3 09:02:18 2012
@@ -2,5 +2,6 @@
.classpath
.project
target
+.idea
*.i??
velocity.log
\ No newline at end of file
Modified: servicemix/smx4/features/trunk/osgi-logging/jms-appender/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/pom.xml?rev=1393356&r1=1393355&r2=1393356&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/osgi-logging/jms-appender/pom.xml (original)
+++ servicemix/smx4/features/trunk/osgi-logging/jms-appender/pom.xml Wed Oct 3
09:02:18 2012
@@ -34,14 +34,14 @@
<packaging>bundle</packaging>
<dependencies>
- <dependency>
+ <dependency>
<groupId>org.ops4j.pax.logging</groupId>
- <artifactId>pax-logging-api</artifactId>
+ <artifactId>pax-logging-service</artifactId>
<version>${pax.logging.version}</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.logging</groupId>
- <artifactId>pax-logging-service</artifactId>
+ <artifactId>pax-logging-api</artifactId>
<version>${pax.logging.version}</version>
</dependency>
<dependency>
@@ -49,6 +49,27 @@
<artifactId>activemq-core</artifactId>
<version>${activemq.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.json</groupId>
+ <artifactId>json</artifactId>
+ <version>20090211</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-camel</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
@@ -61,16 +82,14 @@
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Import-Package>
- org.ops4j.pax.logging;resolution:=optional,
- org.osgi.service.blueprint;resolution:=optional,
- org.ops4j.pax.logging.spi*;resolution:=optional,
- org.slf4j,
- javax.jms,
- org.apache.activemq
+ *
</Import-Package>
- <Export-Package>
- org.apache.servicemix.logging
- </Export-Package>
+ <Private-Package>
+ org.apache.servicemix.logging.jms
+ </Private-Package>
+ <Embed-Dependency>
+ json
+ </Embed-Dependency>
</instructions>
</configuration>
</plugin>
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/DefaultLoggingEventFormat.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/DefaultLoggingEventFormat.java?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/DefaultLoggingEventFormat.java
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/DefaultLoggingEventFormat.java
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.logging.jms;
+
+import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * Default event logging format for the JMS appender
+ */
+public class DefaultLoggingEventFormat implements LoggingEventFormat {
+
+ private SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+
+ public String toString(PaxLoggingEvent paxLoggingEvent) {
+ StringBuilder writer = new StringBuilder();
+
+ writer.append("Error");
+ writer.append(",\n \"timestamp\" : " +
formatDate(paxLoggingEvent.getTimeStamp()));
+ writer.append(",\n \"level\" : " +
paxLoggingEvent.getLevel().toString());
+ writer.append(",\n \"logger\" : " + paxLoggingEvent.getLoggerName());
+ writer.append(",\n \"thread\" : " + paxLoggingEvent.getThreadName());
+ writer.append(",\n \"message\" : " + paxLoggingEvent.getMessage());
+
+ String[] throwable = paxLoggingEvent.getThrowableStrRep();
+ if (throwable != null) {
+ writer.append(",\n \"exception\" : [");
+ for (int i = 0; i < throwable.length; i++) {
+ if (i != 0)
+ writer.append(", " + throwable[i]);
+ }
+ writer.append("]");
+ }
+
+ writer.append(",\n \"properties\" : { ");
+ boolean first = true;
+ for (Object key : paxLoggingEvent.getProperties().keySet()) {
+ if (first) {
+ first = false;
+ } else {
+ writer.append(", ");
+ }
+ writer.append("key : " + key.toString());
+ writer.append(": " +
paxLoggingEvent.getProperties().get(key).toString());
+ }
+ writer.append(" }");
+ writer.append("\n}");
+ return writer.toString();
+ }
+
+ private String formatDate(long timestamp) {
+ return simpleDateFormat.format(new Date(timestamp));
+ }
+}
Copied:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
(from r1386914,
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/JMSAppender.java)
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java?p2=servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java&p1=servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/JMSAppender.java&r1=1386914&r2=1393356&rev=1393356&view=diff
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/JMSAppender.java
(original)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/JMSAppender.java
Wed Oct 3 09:02:18 2012
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.servicemix.logging;
+package org.apache.servicemix.logging.jms;
import org.ops4j.pax.logging.spi.PaxAppender;
import org.ops4j.pax.logging.spi.PaxLoggingEvent;
@@ -22,13 +22,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
-import java.text.SimpleDateFormat;
-import java.util.Date;
public class JMSAppender implements PaxAppender {
private static final transient Logger LOG =
LoggerFactory.getLogger(JMSAppender.class);
+ private static final String DEFAULT_EVENT_FORMAT = "default";
+ private static final String LOGSTASH_EVENT_FORMAT = "logstash";
+
+
private ConnectionFactory jmsConnectionFactory;
private Connection connection;
private Session session;
@@ -36,13 +38,14 @@ public class JMSAppender implements PaxA
private Topic topic;
private String destinationName;
- private SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+ private LoggingEventFormat format = new DefaultLoggingEventFormat();
+
+
public void init() {
/*
* Create connection. Create session from connection; false means
* session is not transacted.
- * Finally, close connection.
*/
try {
connection = jmsConnectionFactory.createConnection();
@@ -70,56 +73,14 @@ public class JMSAppender implements PaxA
}
public void doAppend(PaxLoggingEvent paxLoggingEvent) {
-
try {
- StringBuilder writer = new StringBuilder();
-
- writer.append("Error");
- writer.append(",\n \"timestamp\" : " +
formatDate(paxLoggingEvent.getTimeStamp()));
- writer.append(",\n \"level\" : " +
paxLoggingEvent.getLevel().toString());
- writer.append(",\n \"logger\" : " +
paxLoggingEvent.getLoggerName());
- writer.append(",\n \"thread\" : " +
paxLoggingEvent.getThreadName());
- writer.append(",\n \"message\" : " +
paxLoggingEvent.getMessage());
-
- String[] throwable = paxLoggingEvent.getThrowableStrRep();
- if (throwable != null) {
- writer.append(",\n \"exception\" : [");
- for (int i = 0; i < throwable.length; i++) {
- if (i != 0)
- writer.append(", " + throwable[i]);
- }
- writer.append("]");
- }
-
- writer.append(",\n \"properties\" : { ");
- boolean first = true;
- for (Object key : paxLoggingEvent.getProperties().keySet()) {
- if (first) {
- first = false;
- } else {
- writer.append(", ");
- }
- writer.append("key : " + key.toString());
- writer.append(": " +
paxLoggingEvent.getProperties().get(key).toString());
- }
- writer.append(" }");
- writer.append("\n}");
-
// Send message to the destination
TextMessage message = session.createTextMessage();
- message.setText(writer.toString());
+ message.setText(format.toString(paxLoggingEvent));
publisher.send(message);
-
- // System.out.println(">> Message created : " + writer.toString());
-
- } catch (Exception e) {
+ } catch (JMSException e) {
e.printStackTrace();
}
-
- }
-
- private String formatDate(long timestamp) {
- return simpleDateFormat.format(new Date(timestamp));
}
public void setJmsConnectionFactory(ConnectionFactory
jmsConnectionFactory) {
@@ -130,4 +91,11 @@ public class JMSAppender implements PaxA
this.destinationName = destinationName;
}
+ public void setFormat(String name) {
+ if (LOGSTASH_EVENT_FORMAT.equals(name)) {
+ format = new LogstashEventFormat();
+ } else {
+ format = new DefaultLoggingEventFormat();
+ }
+ }
}
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LoggingEventFormat.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LoggingEventFormat.java?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LoggingEventFormat.java
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LoggingEventFormat.java
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,28 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.logging.jms;
+
+import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+
+/**
+ * Interface to represent an event message format, used for serializing log
events into JMS messages
+ */
+public interface LoggingEventFormat {
+
+ public String toString(PaxLoggingEvent event);
+
+}
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LogstashEventFormat.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LogstashEventFormat.java?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LogstashEventFormat.java
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/java/org/apache/servicemix/logging/jms/LogstashEventFormat.java
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,68 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.logging.jms;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * Creates a log message in Logstash' internal message format,
+ * cfr.
https://github.com/logstash/logstash/wiki/logstash's-internal-message-format
+ */
+public class LogstashEventFormat implements LoggingEventFormat {
+
+ protected static final DateFormat TIMESTAMP_FORMAT = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
+
+ protected static final String FIELDS = "@fields";
+ protected static final String MESSAGE = "@message";
+ protected static final String SOURCE = "@source";
+ protected static final String TAGS = "@tags";
+ protected static final String TIMESTAMP = "@timestamp";
+
+ public String toString(PaxLoggingEvent event) {
+ JSONObject object = new JSONObject();
+ try {
+ object.put(MESSAGE, event.getMessage());
+ object.put(SOURCE, event.getLoggerName());
+ object.put(TIMESTAMP, TIMESTAMP_FORMAT.format(new
Date(event.getTimeStamp())));
+
+ JSONObject fields = new JSONObject();
+ for (Object property : event.getProperties().entrySet()) {
+ Map.Entry<String, Object> entry = (Map.Entry<String, Object>)
property;
+ fields.put(entry.getKey(), entry.getValue().toString());
+ }
+
+ object.put(FIELDS, fields);
+
+ JSONArray tags = new JSONArray();
+ tags.put(event.getLevel().toString());
+ object.put(TAGS, tags);
+ } catch (JSONException e) {
+ // let's return a minimal, String-based message representation
instead
+ return "{ \"" + MESSAGE + "\" : " + event.getMessage() + "}";
+ }
+ return object.toString();
+ }
+
+}
Modified:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml?rev=1393356&r1=1393355&r2=1393356&view=diff
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
(original)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/main/resources/OSGI-INF/blueprint/config.xml
Wed Oct 3 09:02:18 2012
@@ -25,9 +25,10 @@
<!-- Need OSGI JMS Connection Factory Service exposed -->
<reference id="jmsConnectionFactory"
interface="javax.jms.ConnectionFactory"/>
- <bean id="appender" class="org.apache.servicemix.logging.JMSAppender"
init-method="init" destroy-method="close">
+ <bean id="appender" class="org.apache.servicemix.logging.jms.JMSAppender"
init-method="init" destroy-method="close">
<property name="jmsConnectionFactory" ref="jmsConnectionFactory"/>
<property name="destinationName" value="${destinationName}" />
+ <property name="format" value="${format}"/>
</bean>
<service ref="appender" interface="org.ops4j.pax.logging.spi.PaxAppender">
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/JMSAppenderTest.java
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.logging.jms;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.camel.component.ActiveMQComponent;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.spi.LoggingEvent;
+import org.junit.Before;
+import org.junit.Test;
+import org.ops4j.pax.logging.service.internal.PaxLoggingEventImpl;
+import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+
+import javax.naming.Context;
+
+/**
+ * Test cases for {@link JMSAppender}
+ */
+public class JMSAppenderTest extends CamelTestSupport {
+
+ private static final String BROKER_URL =
"vm://test.broker?broker.persistent=false";
+ private static final String EVENTS_TOPIC = "Events";
+
+ private JMSAppender appender;
+
+ @Before
+ public void setupBrokerAndAppender() throws Exception {
+ appender = new JMSAppender();
+ appender.setJmsConnectionFactory(new
ActiveMQConnectionFactory(BROKER_URL));
+ appender.setDestinationName(EVENTS_TOPIC);
+ appender.init();
+ }
+
+ @Test
+ public void testLogstashAppender() throws InterruptedException {
+ MockEndpoint events = getMockEndpoint("mock:events");
+ events.expectedMessageCount(1);
+
+ appender.doAppend(MockEvents.createInfoEvent());
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected Context createJndiContext() throws Exception {
+ Context context = super.createJndiContext();
+ context.bind("amq", ActiveMQComponent.activeMQComponent(BROKER_URL));
+ return context;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("amq:topic://" + EVENTS_TOPIC).to("mock:events");
+ }
+ };
+ }
+}
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/LogstashEventFormatTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/LogstashEventFormatTest.java?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/LogstashEventFormatTest.java
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/LogstashEventFormatTest.java
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,50 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.logging.jms;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Test cases for the {@link LogstashEventFormat} class
+ */
+public class LogstashEventFormatTest {
+
+ private final LoggingEventFormat format = new LogstashEventFormat();
+
+ @Test
+ public void testBasicLogstashFormat() throws JSONException {
+ PaxLoggingEvent event = MockEvents.createInfoEvent();
+
+ JSONObject object = new JSONObject(format.toString(event));
+ assertEquals(MockEvents.LOG_MESSAGE,
object.get(LogstashEventFormat.MESSAGE));
+ assertEquals(MockEvents.LOGGER_NAME,
object.get(LogstashEventFormat.SOURCE));
+ assertEquals("INFO",
object.getJSONArray(LogstashEventFormat.TAGS).get(0));
+ assertEquals(MockEvents.LOG_PROPERTY_VALUE,
+
object.getJSONObject(LogstashEventFormat.FIELDS).get(MockEvents.LOG_PROPERTY_ID));
+ assertNotNull(object.get(LogstashEventFormat.TIMESTAMP));
+
+ System.out.println(object);
+ }
+
+
+}
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/java/org/apache/servicemix/logging/jms/MockEvents.java
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.logging.jms;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.spi.LoggingEvent;
+import org.ops4j.pax.logging.service.internal.PaxLoggingEventImpl;
+import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+
+/**
+ * Helper class to create mock {@link PaxLoggingEvent}s for testing
+ */
+public class MockEvents {
+
+ public static final String LOGGER_NAME = MockEvents.class.getName();
+ public static final String LOG_MESSAGE = "Important message about your
application!";
+
+ public static final String LOG_PROPERTY_ID = "property.id";
+ public static final String LOG_PROPERTY_VALUE = "property.value";
+
+ protected static PaxLoggingEvent createInfoEvent() {
+ return createInfoEvent(LOGGER_NAME, LOG_MESSAGE);
+ }
+
+ protected static PaxLoggingEvent createInfoEvent(String name, String
message) {
+ Logger logger = Logger.getLogger(name);
+
+ return createEvent(logger, Level.INFO, message, null);
+ }
+
+ private static PaxLoggingEvent createEvent(Logger logger, Level level,
String message, Exception exception) {
+ LoggingEvent event = new LoggingEvent(logger.getName(), logger, level,
message, exception);
+ event.setProperty(LOG_PROPERTY_ID, LOG_PROPERTY_VALUE);
+ return new PaxLoggingEventImpl(event);
+ }
+
+
+}
Added:
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/resources/log4j.properties
URL:
http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/resources/log4j.properties?rev=1393356&view=auto
==============================================================================
---
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/resources/log4j.properties
(added)
+++
servicemix/smx4/features/trunk/osgi-logging/jms-appender/src/test/resources/log4j.properties
Wed Oct 3 09:02:18 2012
@@ -0,0 +1,33 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+# The logging properties used during tests..
+#
+log4j.rootLogger=DEBUG, out
+
+# CONSOLE appender not used by default
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1}
- %m%n
+
+# File appender
+log4j.appender.out=org.apache.log4j.FileAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} -
%m%n
+log4j.appender.out.file=target/jms-appender.log
+log4j.appender.out.append=true