[KARAF-6047] Set the marshaller in the log appender 66/head
authorJean-Baptiste Onofré <jbonofre@apache.org>
Tue, 18 Dec 2018 12:39:45 +0000 (13:39 +0100)
committerJean-Baptiste Onofré <jbonofre@apache.org>
Tue, 18 Dec 2018 13:18:20 +0000 (14:18 +0100)
appender/log/pom.xml
appender/log/src/main/cfg/org.apache.karaf.decanter.appender.log.cfg [new file with mode: 0644]
appender/log/src/main/java/org/apache/karaf/decanter/appender/log/LogAppender.java
assembly/src/main/feature/feature.xml
marshaller/raw/src/main/java/org/apache/karaf/decanter/marshaller/raw/RawMarshaller.java

index 978172a..eb7c90d 100644 (file)
     <packaging>bundle</packaging>
     <name>Apache Karaf :: Decanter :: Appender :: Log</name>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.karaf.decanter</groupId>
+            <artifactId>org.apache.karaf.decanter.api</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>build-helper-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>attach-artifact</goal>
+                        </goals>
+                        <configuration>
+                            <artifacts>
+                                <artifact>
+                                    <file>src/main/cfg/org.apache.karaf.decanter.appender.log.cfg</file>
+                                    <type>cfg</type>
+                                </artifact>
+                            </artifacts>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>
\ No newline at end of file
diff --git a/appender/log/src/main/cfg/org.apache.karaf.decanter.appender.log.cfg b/appender/log/src/main/cfg/org.apache.karaf.decanter.appender.log.cfg
new file mode 100644 (file)
index 0000000..a96f613
--- /dev/null
@@ -0,0 +1,25 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+######################################
+# Decanter Log Appender Configuration
+######################################
+
+# Marshaller
+#marshaller.target=(dataFormat=raw)
\ No newline at end of file
index 9e648c0..75c62c3 100644 (file)
  */
 package org.apache.karaf.decanter.appender.log;
 
+import org.apache.karaf.decanter.api.marshaller.Marshaller;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventConstants;
 import org.osgi.service.event.EventHandler;
@@ -37,13 +40,20 @@ public class LogAppender implements EventHandler {
 
     private final Logger LOGGER = LoggerFactory.getLogger(LogAppender.class);
 
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL)
+    public Marshaller marshaller;
+
     @Override
     public void handleEvent(Event event) {
-        StringBuilder builder = new StringBuilder();
-        for (String innerKey : event.getPropertyNames()) {
-            builder.append(innerKey).append(":").append(toString(event.getProperty(innerKey))).append(" | ");
+        if (marshaller != null) {
+            LOGGER.info(marshaller.marshal(event));
+        } else {
+            StringBuilder builder = new StringBuilder();
+            for (String innerKey : event.getPropertyNames()) {
+                builder.append(innerKey).append(":").append(toString(event.getProperty(innerKey))).append(" | ");
+            }
+            LOGGER.info(builder.toString());
         }
-        LOGGER.info(builder.toString());
     }
 
     private Object toString(Object value) {
index 04aa544..6996590 100644 (file)
 
     <feature name="decanter-appender-log" version="${project.version}" description="Karaf Decanter Log Appender">
         <feature>decanter-common</feature>
+        <configfile finalname="/etc/org.apache.karaf.decanter.appender.log.cfg">mvn:org.apache.karaf.decanter.appender/org.apache.karaf.decanter.appender.log/${project.version}/cfg</configfile>
         <bundle>mvn:org.apache.karaf.decanter.appender/org.apache.karaf.decanter.appender.log/${project.version}</bundle>
     </feature>
 
index cdfe50d..57f474a 100644 (file)
@@ -50,7 +50,9 @@ public class RawMarshaller implements Marshaller {
         StringBuilder builder = new StringBuilder();
         for (String propertyName : event.getPropertyNames()) {
             Object propertyValue = event.getProperty(propertyName);
-            builder.append(propertyName).append("=").append(propertyValue.toString()).append("\n");
+            if (propertyName != null && propertyValue != null) {
+                builder.append(propertyName).append("=").append(propertyValue.toString()).append("\n");
+            }
         }
         return builder.toString();
     }