logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1432555 - in /logging/log4j/log4j2/trunk/core/src: main/java/org/apache/logging/log4j/core/layout/ test/java/org/apache/logging/log4j/core/layout/
Date Sun, 13 Jan 2013 05:08:56 GMT
Author: ggregory
Date: Sun Jan 13 05:08:55 2013
New Revision: 1432555

URL: http://svn.apache.org/viewvc?rev=1432555&view=rev
Log:
Refactor duplicate code in the layouts into the abstract superclass AbstractStringLayout.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.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/RFC5424LayoutTest.java

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java?rev=1432555&r1=1432554&r2=1432555&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java
Sun Jan 13 05:08:55 2013
@@ -27,6 +27,30 @@ import java.nio.charset.Charset;
 public abstract class AbstractStringLayout extends AbstractLayout<String> {
 
     /**
+     * Gets a Charset, starting with the preferred {@code charsetName} if supported, if not,
use UTF-8, if not supported, use the platform
+     * default.
+     * 
+     * @param charsetName
+     *            the preferred charset name
+     * @return a Charset, not null.
+     */
+    public static Charset getSupportedCharset(final String charsetName) {
+        Charset charset = null;
+        if (charsetName != null) {
+            if (Charset.isSupported(charsetName)) {
+                charset = Charset.forName(charsetName);
+            }
+        }
+        if (charset == null) {
+            charset = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
+            if (charsetName != null) {
+                LOGGER.error("Charset " + charsetName + " is not supported for layout, using
" + charset.displayName());
+            }
+        }
+        return charset;
+    }
+
+    /**
      * The charset of the formatted message.
      */
     private final Charset charset;

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java?rev=1432555&r1=1432554&r2=1432555&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
Sun Jan 13 05:08:55 2013
@@ -300,14 +300,7 @@ public final class HTMLLayout extends Ab
                                           @PluginAttr("charset") final String charsetName,
                                           @PluginAttr("fontSize") String fontSize,
                                           @PluginAttr("fontName") String font) {
-        Charset charset = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
-        if (charsetName != null) {
-            if (Charset.isSupported(charsetName)) {
-                charset = Charset.forName(charsetName);
-            } else {
-                LOGGER.error("Charset " + charsetName + " is not supported for layout, using
" + charset.displayName());
-            }
-        }
+        final Charset charset = getSupportedCharset(charsetName);
         if (font == null) {
             font = "arial,sans-serif";
         }

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java?rev=1432555&r1=1432554&r2=1432555&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
Sun Jan 13 05:08:55 2013
@@ -177,14 +177,7 @@ public final class PatternLayout extends
                                              @PluginConfiguration final Configuration config,
                                              @PluginElement("replace") final RegexReplacement
replace,
                                              @PluginAttr("charset") final String charsetName)
{
-        Charset charset = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
-        if (charsetName != null) {
-            if (Charset.isSupported(charsetName)) {
-                charset = Charset.forName(charsetName);
-            } else {
-                LOGGER.error("Charset " + charsetName + " is not supported for layout, using
" + charset.displayName());
-            }
-        }
+        final Charset charset = getSupportedCharset(charsetName);
         return new PatternLayout(config, replace, pattern == null ? DEFAULT_CONVERSION_PATTERN
: pattern, charset);
     }
 }

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java?rev=1432555&r1=1432554&r2=1432555&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
Sun Jan 13 05:08:55 2013
@@ -471,19 +471,7 @@ public final class RFC5424Layout extends
                                              @PluginAttr("charset") final String charsetName,
                                              @PluginAttr("exceptionPattern") final String
exceptionPattern,
                                              @PluginConfiguration final Configuration config)
{
-        Charset charset = null;
-        if (charsetName != null) {
-            if (Charset.isSupported(charsetName)) {
-                charset = Charset.forName(charsetName);
-            } 
-        }
-        if (charset == null) {
-            charset = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
-            if (charsetName != null) {
-                LOGGER.error("Charset " + charsetName + " is not supported for layout, using
" + charset.displayName());
-            }
-        }
-        
+        final Charset charset = getSupportedCharset(charsetName);
         if (includes != null && excludes != null) {
             LOGGER.error("mdcIncludes and mdcExcludes are mutually exclusive. Includes wil
be ignored");
             includes = null;

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java?rev=1432555&r1=1432554&r2=1432555&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java
Sun Jan 13 05:08:55 2013
@@ -133,15 +133,7 @@ public class SyslogLayout extends Abstra
                                             @PluginAttr("newLine") final String includeNL,
                                             @PluginAttr("newLineEscape") final String escapeNL,
                                             @PluginAttr("charset") final String charsetName)
{
-
-        Charset charset = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
-        if (charsetName != null) {
-            if (Charset.isSupported(charsetName)) {
-                charset = Charset.forName(charsetName);
-            } else {
-                LOGGER.error("Charset " + charsetName + " is not supported for layout, using
" + charset.displayName());
-            }
-        }
+        final Charset charset = getSupportedCharset(charsetName);
         final boolean includeNewLine = includeNL == null ? false : Boolean.valueOf(includeNL);
         final Facility f = Facility.toFacility(facility, Facility.LOCAL0);
         return new SyslogLayout(f, includeNewLine, escapeNL, charset);

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=1432555&r1=1432554&r2=1432555&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
Sun Jan 13 05:08:55 2013
@@ -261,14 +261,7 @@ public class XMLLayout extends AbstractS
                                          @PluginAttr("properties") final String properties,
                                          @PluginAttr("complete") final String complete,
                                          @PluginAttr("charset") final String charsetName)
{
-        Charset charset = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
-        if (charsetName != null) {
-            if (Charset.isSupported(charsetName)) {
-                charset = Charset.forName(charsetName);
-            } else {
-                LOGGER.error("Charset " + charsetName + " is not supported for layout, using
" + charset.displayName());
-            }
-        }
+        final Charset charset = getSupportedCharset(charsetName);
         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/RFC5424LayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/RFC5424LayoutTest.java?rev=1432555&r1=1432554&r2=1432555&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/RFC5424LayoutTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/RFC5424LayoutTest.java
Sun Jan 13 05:08:55 2013
@@ -76,7 +76,7 @@ public class RFC5424LayoutTest {
             root.removeAppender(appender);
         }
         // set up appender
-        final RFC5424Layout layout = RFC5424Layout.createLayout("Local0", "Event", "3692",
"true", "RequestContext",
+        final AbstractStringLayout layout = RFC5424Layout.createLayout("Local0", "Event",
"3692", "true", "RequestContext",
             "true", null, "ATM", null, "key1, key2, locale", null, "loginId", null, null,
null);
         final ListAppender appender = new ListAppender("List", null, layout, true, false);
 
@@ -140,7 +140,7 @@ public class RFC5424LayoutTest {
             root.removeAppender(appender);
         }
         // set up appender
-        final RFC5424Layout layout = RFC5424Layout.createLayout("Local0", "Event", "3692",
"true", "RequestContext",
+        final AbstractStringLayout layout = RFC5424Layout.createLayout("Local0", "Event",
"3692", "true", "RequestContext",
             "true", null, "ATM", null, "key1, key2, locale", null, "loginId", null, "%xEx",
null);
         final ListAppender appender = new ListAppender("List", null, layout, true, false);
         appender.start();



Mime
View raw message