logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1389229 - in /logging/log4j/log4j2/trunk: api/src/main/java/org/apache/logging/log4j/message/ core/src/main/java/org/apache/logging/log4j/core/layout/ core/src/test/java/org/apache/logging/log4j/core/ core/src/test/resources/ src/changes/
Date Mon, 24 Sep 2012 07:16:07 GMT
Author: rgoers
Date: Mon Sep 24 07:16:06 2012
New Revision: 1389229

URL: http://svn.apache.org/viewvc?rev=1389229&view=rev
Log:
Add getFormats to MultiformatMessage. Allow StructuredDataMessage to be formatted as XML

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java
    logging/log4j/log4j2/trunk/core/src/test/resources/xml-events.xml
Modified:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java?rev=1389229&r1=1389228&r2=1389229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
Mon Sep 24 07:16:06 2012
@@ -59,6 +59,15 @@ public class MapMessage implements Multi
         this.data = map;
     }
 
+    public String[] getFormats() {
+        String[] formats = new String[MapFormat.values().length];
+        int i = 0;
+        for (MapFormat format : MapFormat.values()) {
+            formats[i++] = format.name();
+        }
+        return formats;
+    }
+
     /**
      * Return the data elements as if they were parameters on the logging event.
      * @return the data elements.

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java?rev=1389229&r1=1389228&r2=1389229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
Mon Sep 24 07:16:06 2012
@@ -33,4 +33,10 @@ public interface MultiformatMessage exte
      * @return The message String.
      */
     String getFormattedMessage(String[] formats);
+
+    /**
+     * Returns the supported formats.
+     * @return The supported formats.
+     */
+    String[] getFormats();
 }

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java?rev=1389229&r1=1389228&r2=1389229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
Mon Sep 24 07:16:06 2012
@@ -23,10 +23,6 @@ import java.util.Map;
  * Represents a Message that conforms to RFC 5424 (http://tools.ietf.org/html/rfc5424).
  */
 public class StructuredDataMessage extends MapMessage implements MultiformatMessage, Serializable
{
-    /**
-     * Full message format includes the type and message.
-     */
-    public static final String FULL = "FULL";
 
     private static final long serialVersionUID = 1703221292892071920L;
     private static final int MAX_LENGTH = 32;
@@ -38,6 +34,13 @@ public class StructuredDataMessage exten
 
     private String type;
 
+    public enum Format {
+        /** The map should be formatted as XML. */
+        XML,
+        /** Full message format includes the type and message. */
+        FULL
+    }
+
     /**
      * Constructor based on a String id.
      * @param id The String id.
@@ -113,6 +116,19 @@ public class StructuredDataMessage exten
     }
 
     /**
+     * Return the supported formats.
+     * @return An array of the supported format names.
+     */
+    public String[] getFormats() {
+        String[] formats = new String[Format.values().length];
+        int i = 0;
+        for (Format format : Format.values()) {
+            formats[i++] = format.name();
+        }
+        return formats;
+    }
+
+    /**
      * Return the id.
      * @return the StructuredDataId.
      */
@@ -178,7 +194,7 @@ public class StructuredDataMessage exten
      */
     @Override
     public String asString() {
-        return asString(FULL, null);
+        return asString(Format.FULL, null);
     }
 
     /**
@@ -189,7 +205,11 @@ public class StructuredDataMessage exten
      */
 
     public String asString(String format) {
-        return asString(format, null);
+        try {
+            return asString(Format.valueOf(format.toUpperCase()), null);
+        } catch (IllegalArgumentException ex) {
+            return asString();
+        }
     }
 
     /**
@@ -201,9 +221,9 @@ public class StructuredDataMessage exten
      *                         will be used.
      * @return The formatted String.
      */
-    public final String asString(String format, StructuredDataId structuredDataId) {
+    public final String asString(Format format, StructuredDataId structuredDataId) {
         StringBuilder sb = new StringBuilder();
-        boolean full = FULL.equals(format);
+        boolean full = Format.FULL.equals(format);
         if (full) {
             String type = getType();
             if (type == null) {
@@ -240,7 +260,7 @@ public class StructuredDataMessage exten
      */
     @Override
     public String getFormattedMessage() {
-        return asString(FULL, null);
+        return asString(Format.FULL, null);
     }
 
     /**
@@ -256,14 +276,30 @@ public class StructuredDataMessage exten
     public String getFormattedMessage(String[] formats) {
         if (formats != null && formats.length > 0) {
             for (String format : formats) {
-                if (format.equalsIgnoreCase(FULL)) {
-                    return asString(FULL, null);
+                if (Format.XML.name().equalsIgnoreCase(format)) {
+                    return asXML();
+                } else if (Format.FULL.name().equalsIgnoreCase(format)) {
+                    return asString(Format.FULL, null);
                 }
             }
             return asString(null, null);
         } else {
-            return asString(FULL, null);
+            return asString(Format.FULL, null);
+        }
+    }
+
+    private String asXML() {
+        StringBuilder sb = new StringBuilder();
+        StructuredDataId id = getId();
+        if (id == null || id.getName() == null || type == null) {
+            return sb.toString();
         }
+        sb.append("<StructuredData>\n");
+        sb.append("<type>").append(type).append("</type>\n");
+        sb.append("<id>").append(id).append("</id>\n");
+        super.asXML(sb);
+        sb.append("</StructuredData>\n");
+        return sb.toString();
     }
 
     @Override

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java?rev=1389229&r1=1389228&r2=1389229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
Mon Sep 24 07:16:06 2012
@@ -32,6 +32,8 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.helpers.Transform;
 import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.MultiformatMessage;
 
 
 /**
@@ -77,6 +79,8 @@ public class XMLLayout extends AbstractS
     private final boolean properties;
     private final boolean complete;
 
+    private static final String[] FORMATS = new String[] {"xml"};
+
     protected XMLLayout(boolean locationInfo, boolean properties, boolean complete, Charset
charset) {
         super(charset);
         this.locationInfo = locationInfo;
@@ -108,13 +112,31 @@ public class XMLLayout extends AbstractS
         buf.append(Transform.escapeTags(event.getThreadName()));
         buf.append("\">\r\n");
 
-        buf.append("<log4j:message><![CDATA[");
-        // Append the rendered message. Also make sure to escape any
-        // existing CDATA sections.
-        Transform.appendEscapingCDATA(buf, event.getMessage().getFormattedMessage());
-        buf.append("]]></log4j:message>\r\n");
+        Message msg = event.getMessage();
+        if (msg != null) {
+            boolean xmlSupported = false;
+            if (msg instanceof MultiformatMessage) {
+                String[] formats = ((MultiformatMessage) msg).getFormats();
+                for (String format : formats) {
+                    if (format.equalsIgnoreCase("XML")) {
+                        xmlSupported = true;
+                    }
+                }
+            }
+            if (xmlSupported) {
+                buf.append("<log4j:message>");
+                buf.append(((MultiformatMessage) msg).getFormattedMessage(FORMATS));
+                buf.append("</log4j:message>");
+            } else {
+                buf.append("<log4j:message><![CDATA[");
+                // Append the rendered message. Also make sure to escape any
+                // existing CDATA sections.
+                Transform.appendEscapingCDATA(buf, event.getMessage().getFormattedMessage());
+                buf.append("]]></log4j:message>\r\n");
+            }
+        }
 
-        if (event.getContextStack().size() > 0) {
+        if (event.getContextStack() != null && event.getContextStack().size() >
0) {
             buf.append("<log4j:NDC><![CDATA[");
             Transform.appendEscapingCDATA(buf, event.getContextStack().toString());
             buf.append("]]></log4j:NDC>\r\n");

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java?rev=1389229&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java
Mon Sep 24 07:16:06 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.logging.log4j.core;
+
+import org.apache.logging.log4j.EventLogger;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.Locale;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class XMLEvents {
+
+    private static final String CONFIG = "xml-events.xml";
+
+    @BeforeClass
+    public static void setupClass() {
+        System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+    }
+
+    @Test
+    public void testEvents() {
+        ThreadContext.put("loginId", "JohnDoe");
+        ThreadContext.put("ipAddress", "192.168.0.120");
+        ThreadContext.put("locale", Locale.US.getDisplayName());
+        TransferMessage msg = new TransferMessage();
+        msg.put("ToAccount", "123456");
+        msg.put("FromAccount", "123457");
+        msg.put("Amount", "200.00");
+        EventLogger.logEvent(msg);
+        msg.setCompletionStatus("Transfer Complete");
+        EventLogger.logEvent(msg);
+        ThreadContext.clear();
+
+
+    }
+
+    private class TransferMessage extends StructuredDataMessage {
+
+        public TransferMessage() {
+            super("Transfer@18060", null, "Audit");
+        }
+
+        public void setCompletionStatus(String msg) {
+            setMessageFormat(msg);
+        }
+    }
+}

Added: logging/log4j/log4j2/trunk/core/src/test/resources/xml-events.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/xml-events.xml?rev=1389229&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/xml-events.xml (added)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/xml-events.xml Mon Sep 24 07:16:06
2012
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration status="error" name="MyApp" packages="">
+  <appenders>
+    <Console name="STDOUT">
+      <PatternLayout pattern="%d [%p] %c %m%n"/>
+    </Console>
+    <File name="events" fileName="target/events.xml" append="false">
+      <XMLLayout properties="true" complete="true"/>
+    </File>
+  </appenders>
+  <loggers>
+    <logger name="EventLogger" level="info">
+      <appender-ref ref="events"/>
+    </logger>
+    <root level="warn">
+      <appender-ref ref="STDOUT"/>
+    </root>
+  </loggers>
+</configuration>
\ No newline at end of file

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1389229&r1=1389228&r2=1389229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Sep 24 07:16:06 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta2" date="TBD" description="Bug fixes and enhancements">
+      <action dev="rgoers" type="add">
+        Add getFormats to MultiformatMessage and allow StructuredDataMessage to format as
XML.
+      </action>
       <action issue="LOG4J2-90" dev="rgoers" type="fix">
         Add documentation on client vs server mode to performance page.
       </action>



Mime
View raw message