logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1483406 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/helpers/ core/src/main/java/org/apache/logging/log4j/core/layout/ core/src/test/java/org/apache/logging/log4j/core/layout/ src/changes/
Date Thu, 16 May 2013 15:24:48 GMT
Author: ggregory
Date: Thu May 16 15:24:48 2013
New Revision: 1483406

URL: http://svn.apache.org/r1483406
Log:
[LOG4J2-257] XML layout ignores charset for the XML processing intruction's encoding attribute.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Charsets.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Charsets.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Charsets.java?rev=1483406&r1=1483405&r2=1483406&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Charsets.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Charsets.java
Thu May 16 15:24:48 2013
@@ -24,22 +24,35 @@ import org.apache.logging.log4j.status.S
  * Charset utilities.
  */
 public final class Charsets {
-    
-    public static final Charset UTF_8 = Charset.forName("UTF-8");
 
-    private Charsets() {
-    }
+    /**
+     * UTF-8 Charset.
+     */
+    public static final Charset UTF_8 = Charset.forName("UTF-8");
 
     /**
-     * Returns a Charset, if possible the Charset for the specified {@code charsetName},
-     * otherwise (if the specified {@code charsetName} is {@code null} or not supported)
-     * this method returns the platform default Charset.
+     * Returns a Charset, if possible the Charset for the specified {@code charsetName},
otherwise (if the specified
+     * {@code charsetName} is {@code null} or not supported) this method returns the platform
default Charset.
      *
      * @param charsetName
      *            name of the preferred charset or {@code null}
      * @return a Charset, not null.
      */
     public static Charset getSupportedCharset(final String charsetName) {
+        return getSupportedCharset(charsetName, Charset.defaultCharset());
+    }
+
+    /**
+     * Returns a Charset, if possible the Charset for the specified {@code charsetName},
otherwise (if the specified
+     * {@code charsetName} is {@code null} or not supported) this method returns the platform
default Charset.
+     *
+     * @param charsetName
+     *            name of the preferred charset or {@code null}
+     * @param defaultCharset
+     *            returned if {@code charsetName} is null or is not supported.
+     * @return a Charset, never null.
+     */
+    public static Charset getSupportedCharset(final String charsetName, final Charset defaultCharset)
{
         Charset charset = null;
         if (charsetName != null) {
             if (Charset.isSupported(charsetName)) {
@@ -47,13 +60,16 @@ public final class Charsets {
             }
         }
         if (charset == null) {
-            charset = Charset.defaultCharset();
+            charset = defaultCharset;
             if (charsetName != null) {
-                StatusLogger.getLogger().error("Charset " + charsetName + " is not supported
for layout, using " +
-                    charset.displayName());
+                StatusLogger.getLogger().error(
+                        "Charset " + charsetName + " is not supported for layout, using "
+ charset.displayName());
             }
         }
         return charset;
     }
 
+    private Charsets() {
+    }
+
 }

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=1483406&r1=1483405&r2=1483406&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
Thu May 16 15:24:48 2013
@@ -198,9 +198,11 @@ public class XMLLayout extends AbstractS
             return null;
         }
         final StringBuilder sbuf = new StringBuilder();
-        sbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
+        sbuf.append("<?xml version=\"1.0\" encoding=\"");
+        sbuf.append(this.getCharset().name());
+        sbuf.append("\"?>\r\n");
         sbuf.append("<log4j:eventSet xmlns:log4j=\"http://logging.apache.org/log4j/\">\r\n");
-        return sbuf.toString().getBytes(getCharset());
+        return sbuf.toString().getBytes(this.getCharset());
     }
 
 
@@ -267,11 +269,12 @@ public class XMLLayout extends AbstractS
     }
 
     /**
-     * Create an XML Layout.
+     * Creates an XML Layout.
+     * 
      * @param locationInfo If "true" include the location information in the generated XML.
      * @param properties If "true" include the thread context in the generated XML.
      * @param complete If "true" include the XML header.
-     * @param charsetName The character set to use.
+     * @param charsetName The character set to use, if {@code null}, uses UTF-8.
      * @return An XML Layout.
      */
     @PluginFactory
@@ -279,7 +282,7 @@ public class XMLLayout extends AbstractS
                                          @PluginAttr("properties") final String properties,
                                          @PluginAttr("complete") final String complete,
                                          @PluginAttr("charset") final String charsetName)
{
-        final Charset charset = Charsets.getSupportedCharset(charsetName);
+        final Charset charset = Charsets.getSupportedCharset(charsetName, Charsets.UTF_8);
         final boolean info = locationInfo == null ? false : Boolean.valueOf(locationInfo);
         final boolean props = properties == null ? false : Boolean.valueOf(properties);
         final boolean comp = complete == null ? false : Boolean.valueOf(complete);

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java?rev=1483406&r1=1483405&r2=1483406&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
Thu May 16 15:24:48 2013
@@ -92,7 +92,8 @@ public class XMLLayoutTest {
         final List<String> list = appender.getMessages();
 
         assertTrue("Incorrect number of lines. Require at least 50 " + list.size(), list.size()
> 50);
-        assertTrue("Incorrect header", list.get(0).equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
+        final String string = list.get(0);
+        assertTrue("Incorrect header: " + string, string.equals("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>"));
         assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</log4j:eventSet>"));
         assertTrue("Incorrect body. Expected " + body + " Actual: " + list.get(8), list.get(8).equals(body));
     }

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1483406&r1=1483405&r2=1483406&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Thu May 16 15:24:48 2013
@@ -22,6 +22,9 @@
   </properties>
   <body>
     <release version="2.0-beta7" date="2013-??-??" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-257" dev="ggregory" type="fix">
+        XML layout ignores charset for the XML processing intruction's encoding attribute.
+      </action>
       <action issue="LOG4J2-255" dev="rpopma" type="fix">
         Multi-byte character strings are now assumed to be in the platform default encoding,
not UTF-8.
       </action>



Mime
View raw message