logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1185987 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/appender/rolling/ main/java/org/apache/logging/log4j/core/config/ main/java/org/apache/logging/log4j/core/config/p...
Date Wed, 19 Oct 2011 06:37:55 GMT
Author: rgoers
Date: Wed Oct 19 06:37:54 2011
New Revision: 1185987

URL: http://svn.apache.org/viewvc?rev=1185987&view=rev
Log:
Add environment lookup and regex replacement for pattern layout

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java
      - copied, changed from r1183017, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java
      - copied, changed from r1178468, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java
      - copied, changed from r1183017, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
      - copied, changed from r1183017, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/ExtendedThrowablePatternConverterTest.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml
      - copied, changed from r1185679, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-failover.xml
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RelativeTimePatternConverter.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
Wed Oct 19 06:37:54 2011
@@ -182,7 +182,7 @@ public class PatternProcessor {
 
     private PatternParser createPatternParser() {
 
-        return new PatternParser(KEY, null);
+        return new PatternParser(null, KEY, null);
     }
 
     private boolean patternContains(String pattern, char[] chars) {

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
Wed Oct 19 06:37:54 2011
@@ -31,6 +31,8 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.filter.Filterable;
 import org.apache.logging.log4j.core.filter.Filters;
 import org.apache.logging.log4j.core.helpers.NameUtil;
+import org.apache.logging.log4j.core.lookup.Interpolator;
+import org.apache.logging.log4j.core.lookup.StrLookup;
 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 import org.apache.logging.log4j.status.StatusLogger;
 
@@ -74,6 +76,8 @@ public class BaseConfiguration extends F
 
     private boolean started = false;
 
+    private ConcurrentMap<String, Object> componentMap = new ConcurrentHashMap<String,
Object>();
+
     /**
      * Constructor.
      */
@@ -112,6 +116,14 @@ public class BaseConfiguration extends F
     protected void setup() {
     }
 
+    public Object getComponent(String name) {
+        return componentMap.get(name);
+    }
+
+    public void addComponent(String name, Object obj) {
+        componentMap.putIfAbsent(name, obj);
+    }
+
     protected void doConfigure() {
         boolean setRoot = false;
         boolean setLoggers = false;
@@ -121,8 +133,17 @@ public class BaseConfiguration extends F
                 continue;
             }
             if (child.getName().equalsIgnoreCase("properties")) {
-                subst = (StrSubstitutor) child.getObject();
-            } else if (child.getName().equalsIgnoreCase("appenders")) {
+                if (subst.getVariableResolver() == null) {
+                    subst.setVariableResolver((StrLookup) child.getObject());
+                } else {
+                    logger.error("Properties declaration must be the first element in the
configuration");
+                }
+                continue;
+            }
+            else if (subst.getVariableResolver() == null) {
+                subst.setVariableResolver(new Interpolator(null));
+            }
+            if (child.getName().equalsIgnoreCase("appenders")) {
                 appenders = (ConcurrentMap<String, Appender>) child.getObject();
             } else if (child.getName().equalsIgnoreCase("filters")) {
                 setFilters((Filters) child.getObject());

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
Wed Oct 19 06:37:54 2011
@@ -55,4 +55,8 @@ public interface Configuration extends F
     StrSubstitutor getSubst();
 
     void createConfiguration(Node node, LogEvent event);
+
+    Object getComponent(String name);
+
+    void addComponent(String name, Object object);
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
Wed Oct 19 06:37:54 2011
@@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.co
 import org.apache.logging.log4j.core.config.Property;
 import org.apache.logging.log4j.core.lookup.Interpolator;
 import org.apache.logging.log4j.core.lookup.MapLookup;
+import org.apache.logging.log4j.core.lookup.StrLookup;
 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 
 import java.util.HashMap;
@@ -31,9 +32,9 @@ import java.util.Map;
 public class PropertiesPlugin {
 
     @PluginFactory
-    public static StrSubstitutor configureSubstitutor(@PluginElement("properties") Property[]
properties) {
+    public static StrLookup configureSubstitutor(@PluginElement("properties") Property[]
properties) {
         if (properties == null) {
-            return new StrSubstitutor(new Interpolator(null));
+            return new Interpolator(null);
         }
         Map<String, String> map = new HashMap<String, String>();
 
@@ -41,6 +42,6 @@ public class PropertiesPlugin {
             map.put(prop.getName(), prop.getValue());
         }
 
-        return new StrSubstitutor(new Interpolator(new MapLookup(map)));
+        return new Interpolator(new MapLookup(map));
     }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
Wed Oct 19 06:37:54 2011
@@ -18,13 +18,17 @@
 package org.apache.logging.log4j.core.layout;
 
 import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
+import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.helpers.OptionConverter;
 import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
 import org.apache.logging.log4j.core.pattern.PatternConverter;
 import org.apache.logging.log4j.core.pattern.PatternParser;
+import org.apache.logging.log4j.core.pattern.RegexReplacement;
 
 import java.nio.charset.Charset;
 import java.util.List;
@@ -415,7 +419,7 @@ public class PatternLayout extends Abstr
      */
     private List<PatternConverter> converters;
 
-    private static final String KEY = "Converter";
+    public static final String KEY = "Converter";
 
     /**
      * Conversion pattern.
@@ -428,12 +432,19 @@ public class PatternLayout extends Abstr
     private boolean handlesExceptions;
 
     /**
+     * The current Configuration.
+     */
+    private final Configuration config;
+
+    private final RegexReplacement replace;
+
+    /**
      * Constructs a EnhancedPatternLayout using the DEFAULT_LAYOUT_PATTERN.
      * <p/>
      * The default pattern just produces the application supplied message.
      */
     public PatternLayout() {
-        this(DEFAULT_CONVERSION_PATTERN, Charset.defaultCharset());
+        this(null, null, DEFAULT_CONVERSION_PATTERN, Charset.defaultCharset());
     }
 
     /**
@@ -442,7 +453,16 @@ public class PatternLayout extends Abstr
      * The default pattern just produces the application supplied message.
      */
     public PatternLayout(final String pattern) {
-        this(pattern, Charset.defaultCharset());
+        this(null, null, pattern, Charset.defaultCharset());
+    }
+
+   /**
+     * Constructs a EnhancedPatternLayout using the DEFAULT_LAYOUT_PATTERN.
+     * <p/>
+     * The default pattern just produces the application supplied message.
+     */
+    public PatternLayout(Configuration config, final String pattern) {
+        this(config, null, pattern, Charset.defaultCharset());
     }
 
     /**
@@ -450,10 +470,13 @@ public class PatternLayout extends Abstr
      *
      * @param pattern conversion pattern.
      */
-    public PatternLayout(final String pattern, final Charset charset) {
+    public PatternLayout(Configuration config, final RegexReplacement replace, final String
pattern,
+                         final Charset charset) {
         super(charset);
+        this.replace = replace;
         this.conversionPattern = pattern;
-        PatternParser parser = createPatternParser();
+        this.config = config;
+        PatternParser parser = createPatternParser(config);
         converters = parser.parse((pattern == null) ? DEFAULT_CONVERSION_PATTERN : pattern);
         handlesExceptions = parser.handlesExceptions();
 
@@ -471,7 +494,7 @@ public class PatternLayout extends Abstr
         if (pattern == null) {
             return;
         }
-        PatternParser parser = createPatternParser();
+        PatternParser parser = createPatternParser(this.config);
         converters = parser.parse(pattern);
         handlesExceptions = parser.handlesExceptions();
     }
@@ -486,12 +509,24 @@ public class PatternLayout extends Abstr
         for (PatternConverter c : converters) {
             c.format(event, buf);
         }
-        return buf.toString();
+        String str = buf.toString();
+        if (replace != null) {
+            str = replace.format(str);
+        }
+        return config == null ? str : config.getSubst().replace(event, str);
     }
 
-    private PatternParser createPatternParser() {
-
-        return new PatternParser(KEY, LogEventPatternConverter.class);
+    private PatternParser createPatternParser(Configuration config) {
+        if (config == null) {
+            return new PatternParser(config, KEY, LogEventPatternConverter.class);
+        }
+        PatternParser parser = (PatternParser) config.getComponent(KEY);
+        if (parser == null) {
+            parser = new PatternParser(config, KEY, LogEventPatternConverter.class);
+            config.addComponent(KEY, parser);
+            parser = (PatternParser) config.getComponent(KEY);
+        }
+        return parser;
     }
 
     public String toString() {
@@ -500,6 +535,8 @@ public class PatternLayout extends Abstr
 
     @PluginFactory
     public static PatternLayout createLayout(@PluginAttr("pattern") String pattern,
+                                             @PluginConfiguration Configuration config,
+                                             @PluginElement("replace") RegexReplacement replace,
                                              @PluginAttr("charset") String charset) {
         Charset c = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
         if (charset != null) {
@@ -510,7 +547,7 @@ public class PatternLayout extends Abstr
             }
         }
         if (pattern != null) {
-            return new PatternLayout(pattern, c);
+            return new PatternLayout(config, replace, pattern, c);
         }
         logger.error("No pattern specified for PatternLayout");
         return null;

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java
(from r1183017, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java&r1=1183017&r2=1185987&rev=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java
Wed Oct 19 06:37:54 2011
@@ -20,16 +20,27 @@ import org.apache.logging.log4j.core.Log
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 
 /**
- * Looks up keys from system properties
+ * Looks up keys from environment variables.
  */
-@Plugin(name="sys",type="Lookup")
-public class SystemPropertiesLookup implements StrLookup {
+@Plugin(name="env",type="Lookup")
+public class EnvironmentLookup implements StrLookup {
 
+    /**
+     * Get the value of the environment variable.
+     * @param key  the key to be looked up, may be null
+     * @return The value of the environment variable.
+     */
     public String lookup(String key) {
-        return System.getProperty(key);
+        return System.getenv(key);
     }
 
+    /**
+     * Get the value of the environment variable.
+     * @param event The current LogEvent (is ignored by this StrLookup).
+     * @param key  the key to be looked up, may be null
+     * @return The value of the environment variable.
+     */
     public String lookup(LogEvent event, String key) {
-        return System.getProperty(key);
+        return System.getenv(key);
     }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
Wed Oct 19 06:37:54 2011
@@ -40,7 +40,7 @@ public class Interpolator implements Str
     private final StrLookup defaultLookup;
 
     public Interpolator(StrLookup defaultLookup) {
-        this.defaultLookup = defaultLookup;
+        this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String,
String>()) : defaultLookup;
         PluginManager manager = new PluginManager("Lookup");
         manager.collectPlugins();
         Map<String, PluginType> plugins = manager.getPlugins();

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
Wed Oct 19 06:37:54 2011
@@ -27,7 +27,7 @@ import org.apache.logging.log4j.message.
  * Return the event's rendered message in a StringBuffer.
  */
 @Plugin(name="MessagePatternConverter", type="Converter")
-@ConverterKeys({"m", "message"})
+@ConverterKeys({"m", "msg", "message"})
 public final class MessagePatternConverter extends LogEventPatternConverter {
 
     private final String format;

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
Wed Oct 19 06:37:54 2011
@@ -18,6 +18,7 @@
 package org.apache.logging.log4j.core.pattern;
 
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.plugins.PluginManager;
 import org.apache.logging.log4j.core.config.plugins.PluginType;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -71,20 +72,25 @@ public final class PatternParser {
      */
     private boolean handlesExceptions;
 
+    private Configuration config;
+
     private final Map<String, Class<PatternConverter>> converterRules;
 
     protected final static Logger logger = StatusLogger.getLogger();
 
 
     public PatternParser(String converterKey) {
-        this(converterKey, null);
+        this(null, converterKey, null);
     }
 
     /**
      * Constructor
+     * @param config The current Configuration.
      * @param converterKey The key to lookup the converters.
+     * @param expected The expected base Class of each Converter.
      */
-    public PatternParser(String converterKey, Class expected) {
+    public PatternParser(Configuration config, String converterKey, Class expected) {
+        this.config = config;
         PluginManager manager = new PluginManager(converterKey, expected);
         manager.collectPlugins();
         Map<String, PluginType> plugins = manager.getPlugins();
@@ -394,16 +400,49 @@ public final class PatternParser {
             return null;
         }
 
-        try {
-            Method factory = converterClass.getMethod(
-                    "newInstance",
-                    new Class[]{
-                        Class.forName("[Ljava.lang.String;")
-                    });
-            String[] optionsArray = new String[options.size()];
-            optionsArray = options.toArray(optionsArray);
+        Method[] methods = converterClass.getMethods();
+        Method newInstance = null;
+        for (Method method : methods) {
+            if (method.getName().equals("newInstance")) {
+                if (newInstance == null) {
+                    newInstance = method;
+                } else {
+                    logger.error("Class " + converterClass + " cannot contain multiple newInstance
methods");
+                    return null;
+                }
+            }
+        }
+        if (newInstance == null) {
+            logger.error("Class " + converterClass + " does not contain a newInstance method");
+            return null;
+        }
+
+        Class[] parmTypes = newInstance.getParameterTypes();
+        Object [] parms = parmTypes.length > 0 ? new Object[parmTypes.length] : null;
+
+        if (parms != null) {
+            int i = 0;
+            boolean errors = false;
+            for (Class clazz : parmTypes) {
+                if (clazz.isArray() && clazz.getName().equals("[Ljava.lang.String;"))
{
+                    String[] optionsArray = options.toArray(new String[options.size()]);
+                    parms[i] = optionsArray;
+                } else if (clazz.isAssignableFrom(Configuration.class)) {
+                    parms[i] = config;
+                } else {
+                    logger.error("Unknown parameter type " + clazz.getName() + " for newInstance
method of " +
+                        converterClass.getName());
+                    errors = true;
+                }
+                ++i;
+            }
+            if (errors) {
+                return null;
+            }
+        }
 
-            Object newObj = factory.invoke(null, new Object[]{optionsArray});
+        try {
+            Object newObj = newInstance.invoke(null, parms);
 
             if (newObj instanceof PatternConverter) {
                 currentLiteral.delete(0, currentLiteral.length()
@@ -415,18 +454,6 @@ public final class PatternParser {
             }
         } catch (Exception ex) {
             logger.error("Error creating converter for " + converterId, ex);
-
-            try {
-                //
-                //  try default constructor
-                PatternConverter pc = converterClass.newInstance();
-                currentLiteral.delete(0, currentLiteral.length()
-                        - (converterId.length() - converterName.length()));
-
-                return pc;
-            } catch (Exception ex2) {
-                logger.error("Error creating converter for " + converterId, ex2);
-            }
         }
 
         return null;

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java
(from r1178468, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java&r1=1178468&r2=1185987&rev=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java
Wed Oct 19 06:37:54 2011
@@ -17,80 +17,64 @@
 
 package org.apache.logging.log4j.core.pattern;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttr;
+import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.status.StatusLogger;
 
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
+import java.util.List;
+import java.util.regex.Pattern;
 
 /**
- * Able to handle the contents of the LogEvent's MDC and either
- * output the entire contents of the properties in a similar format to the
- * java.util.Hashtable.toString(), or to output the value of a specific key
- * within the property bundle
- * when this pattern converter has the option set.
+ *
  */
- @Plugin(name="MDCPatternConverter", type="Converter")
-@ConverterKeys({"X", "mdc", "MDC"})
-public final class MDCPatternConverter extends LogEventPatternConverter {
-    /**
-     * Name of property to output.
-     */
-    private final String key;
+@Plugin(name="replace", type="Core", printObject=true)
+public final class RegexReplacement {
+
+    private final Pattern pattern;
+
+    private final String substitution;
+
+    private static Logger logger = StatusLogger.getLogger();
 
     /**
      * Private constructor.
      *
      * @param options options, may be null.
      */
-    private MDCPatternConverter(final String[] options) {
-        super(((options != null) && (options.length > 0)) ? ("MDC{" + options[0]
+ "}") : "MDC", "mdc");
-        key = (options != null && options.length > 0) ? options[0] : null;
+    private RegexReplacement(Pattern pattern, String substitution) {
+        this.pattern = pattern;
+        this.substitution = substitution;
     }
 
     /**
-     * Obtains an instance of PropertiesPatternConverter.
-     *
-     * @param options options, may be null or first element contains name of property to
format.
-     * @return instance of PropertiesPatternConverter.
+     * {@inheritDoc}
      */
-    public static MDCPatternConverter newInstance(final String[] options) {
-        return new MDCPatternConverter(options);
+    public String format(String msg) {
+        return pattern.matcher(msg).replaceAll(substitution);
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public void format(final LogEvent event, final StringBuilder toAppendTo) {
-        Map<String, String> contextMap = event.getContextMap();
-        // if there is no additional options, we output every single
-        // Key/Value pair for the MDC in a similar format to Hashtable.toString()
-        if (key == null) {
-
-
-            if (contextMap == null || contextMap.size() == 0) {
-                toAppendTo.append("{}");
-                return;
-            }
-            StringBuilder sb = new StringBuilder("{");
-            Set<String> keys = new TreeSet<String>(contextMap.keySet());
-            for (String key : keys) {
-                if (sb.length() > 1) {
-                    sb.append(", ");
-                }
-                sb.append(key).append("=").append(contextMap.get(key));
-
-            }
-            sb.append("}");
-            toAppendTo.append(sb);
-        } else if (contextMap != null) {
-            // otherwise they just want a single key output
-            Object val = contextMap.get(key);
-
-            if (val != null) {
-                toAppendTo.append(val);
-            }
+    public String toString() {
+        return "replace(regex=" + pattern.pattern() + ", replacement=" + substitution + ")";
+    }
+
+    @PluginFactory
+    public static RegexReplacement createRegexReplacement(@PluginAttr("regex") String regex,
+                                                          @PluginAttr("replacement") String
replacement) {
+        if (regex == null) {
+            logger.error("A regular expression is required for replacement");
+            return null;
+        }
+        if (replacement == null) {
+            logger.error("A replacement string is required to perform replacement");
         }
+        Pattern p = Pattern.compile(regex);
+        return new RegexReplacement(p, replacement);
     }
+
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RelativeTimePatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RelativeTimePatternConverter.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RelativeTimePatternConverter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RelativeTimePatternConverter.java
Wed Oct 19 06:37:54 2011
@@ -20,10 +20,11 @@ package org.apache.logging.log4j.core.pa
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 
+import java.lang.management.ManagementFactory;
+
 
 /**
- * Return the relative time in milliseconds since loading of the LoggingEvent
- * class.
+ * Return the relative time in milliseconds since JVM Startup.
  */
 @Plugin(name="RelativeTimePatternConverter", type="Converter")
 @ConverterKeys({"r", "relative"})
@@ -32,7 +33,7 @@ public class RelativeTimePatternConverte
      * Cached formatted timestamp.
      */
     private long lastTimestamp = Long.MIN_VALUE;
-    private long startTime = System.currentTimeMillis();
+    private long startTime = ManagementFactory.getRuntimeMXBean().getStartTime();
     private String relative;
 
     /**

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java?rev=1185987&r1=1185986&r2=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
Wed Oct 19 06:37:54 2011
@@ -74,7 +74,7 @@ public class PatternLayoutTest {
         String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
 
         // set up appender
-        PatternLayout layout = new PatternLayout(msgPattern);
+        PatternLayout layout = new PatternLayout(ctx.getConfiguration(), msgPattern);
         //FileOutputStream fos = new FileOutputStream(OUTPUT_FILE + "_mdc");
         FileManager manager = FileManager.getFileManager(OUTPUT_FILE + "_mdc", false, false,
false);
         FileAppender appender = new FileAppender("File", layout, null, manager, OUTPUT_FILE
+ "_mdc", true, false);

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java
(from r1183017, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java&r1=1183017&r2=1185987&rev=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java
Wed Oct 19 06:37:54 2011
@@ -20,33 +20,20 @@ import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
 /**
  *
  */
-public class SystemPropertiesLookupTest {
+public class EnvironmentLookupTest {
 
 
-    private static final String TESTKEY = "TestKey";
-    private static final String TESTVAL = "TestValue";
-
-    @BeforeClass
-    public static void before() {
-        System.setProperty(TESTKEY, TESTVAL);
-    }
-
-    @AfterClass
-    public static void after() {
-        System.clearProperty(TESTKEY);
-    }
-
     @Test
     public void testLookup() {
-        StrLookup lookup = new SystemPropertiesLookup();
-        String value = lookup.lookup(TESTKEY);
-        assertEquals(TESTVAL, value);
+        StrLookup lookup = new EnvironmentLookup();
+        String value = lookup.lookup("PATH");
+        assertNotNull(value);
         value = lookup.lookup("BadKey");
         assertNull(value);
     }

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
(from r1183017, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/ExtendedThrowablePatternConverterTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/ExtendedThrowablePatternConverterTest.java&r1=1183017&r2=1185987&rev=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/ExtendedThrowablePatternConverterTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
Wed Oct 19 06:37:54 2011
@@ -16,46 +16,72 @@
  */
 package org.apache.logging.log4j.core.pattern;
 
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent;
-import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.ListAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
+import org.slf4j.MDC;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.util.List;
+import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 /**
  *
  */
-public class ExtendedThrowablePatternConverterTest {
-
-
-
-    @Before
-    public void setup() {
+public class RegexReplacementTest {
+    private static final String CONFIG = "log4j-replace.xml";
+    private static Configuration config;
+    private static ListAppender app;
+    private static LoggerContext ctx;
+
+    @BeforeClass
+    public static void setupClass() {
+        System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+        ctx = (LoggerContext) LogManager.getContext(false);
+        config = ctx.getConfiguration();
+        for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet())
{
+            if (entry.getKey().equals("List")) {
+                app = (ListAppender) entry.getValue();
+                break;
+            }
+        }
+    }
 
+    @AfterClass
+    public static void cleanupClass() {
+        System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+        ctx.reconfigure();
+        StatusLogger.getLogger().reset();
+        ThreadContext.clear();
     }
 
+    org.apache.logging.log4j.Logger logger = LogManager.getLogger("LoggerTest");
+
     @Test
-    public void testFull() {
-        ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null);
-        Throwable cause = new NullPointerException("null pointer");
-        Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
-        LogEvent event = new Log4jLogEvent("testLogger", null, this.getClass().getName(),
Level.DEBUG,
-            new SimpleMessage("test exception"), parent);
-        StringBuilder sb = new StringBuilder();
-        converter.format(event, sb);
-        StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        parent.printStackTrace(pw);
-        String result = sb.toString();
-        //System.out.print(result);
-        result = result.replaceAll(" ~?\\[.*\\]", "");
-        assertEquals(sw.toString(), result);
+    public void testReplacement() {
+        logger.error(this.getClass().getName());
+        List<String> msgs = app.getMessages();
+        assertNotNull(msgs);
+        assertTrue("Incorrect number of messages. Should be 1 is " + msgs.size(), msgs.size()
== 1);
+        assertTrue("Replacement failed", msgs.get(0).endsWith("/RegexReplacementTest\n"));
+        app.clear();
+        ThreadContext.put("MyKey", "Apache");
+        logger.error("This is a test for ${ctx:MyKey}");
+        msgs = app.getMessages();
+        assertNotNull(msgs);
+        assertTrue("Incorrect number of messages. Should be 1 is " + msgs.size(), msgs.size()
== 1);
+        assertEquals("LoggerTest This is a test for Apache\n", msgs.get(0));
     }
 }

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml
(from r1185679, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-failover.xml)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-failover.xml&r1=1185679&r2=1185987&rev=1185987&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-failover.xml
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml
Wed Oct 19 06:37:54 2011
@@ -1,18 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<configuration packages="" status="error" name="FailoverTest">
+<configuration packages="" status="error" name="RegexReplacementTest">
   <Appenders>
-    <AlwaysFail name="Fail" />
-    <List name="List" />
-    <Failover name="Failover" primary="Fail" suppressExceptions="false">
-      <Failovers>
-        <appender-ref ref="List"/>
-      </Failovers>
-    </Failover>
+    <List name="List">
+       <PatternLayout>
+         <replace regex="\." replacement="/"/>
+         <pattern>%logger %msg%n</pattern>
+      </PatternLayout>
+    </List>
   </Appenders>
 
   <loggers>
-    <root level="error">
-      <appender-ref ref="Failover"/>
+    <root level="trace">
+      <appender-ref ref="List"/>
     </root>
   </loggers>
 



Mime
View raw message