You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2013/10/10 20:10:24 UTC

svn commit: r1531064 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/ src...

Author: ggregory
Date: Thu Oct 10 18:10:23 2013
New Revision: 1531064

URL: http://svn.apache.org/r1531064
Log:
[LOG4J2-420] Create a lookup for resource bundle substitution.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java   (with props)
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java   (with props)
    logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/
    logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties   (with props)
    logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties   (with props)
Modified:
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm

Added: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java?rev=1531064&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java Thu Oct 10 18:10:23 2013
@@ -0,0 +1,78 @@
+/*
+ * 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.lookup;
+
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+
+/**
+ * Looks up keys from resource bundles.
+ */
+@Plugin(name = "bundle", category = "Lookup")
+public class ResourceBundleLookup implements StrLookup {
+
+    /**
+     * Looks up the value for the key in the format "BundleName:BundleKey".
+     * 
+     * For example: "com.domain.messages:MyKey".
+     * 
+     * @param key
+     *            the key to be looked up, may be null
+     * @return The value for the key.
+     */
+    @Override
+    public String lookup(final String key) {
+        if (key == null) {
+            return null;
+        }
+        String[] keys = key.split(":");
+        int keyLen = keys.length;
+        if (keyLen != 2) {
+            // throw new IllegalArgumentException("Bad key format " + key + ", format is BundleName:Value");
+            // log?
+            return null;
+        }
+        String bundleName = keys[0];
+        String bundleKey = keys[1];
+        try {
+            // The ResourceBundle class caches bundles, no need to cache here.
+            return ResourceBundle.getBundle(bundleName).getString(bundleKey);
+        } catch (MissingResourceException e) {
+            // log?
+            return null;
+        }
+    }
+
+    /**
+     * Looks up the value for the key in the format "BundleName:BundleKey".
+     * 
+     * For example: "com.domain.messages:MyKey".
+     * 
+     * @param event
+     *            The current LogEvent.
+     * @param key
+     *            the key to be looked up, may be null
+     * @return The value associated with the key.
+     */
+    @Override
+    public String lookup(final LogEvent event, final String key) {
+        return lookup(key);
+    }
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java?rev=1531064&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java Thu Oct 10 18:10:23 2013
@@ -0,0 +1,51 @@
+/*
+ * 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.lookup;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class ResourceBundleLookupTest {
+
+    @Test
+    public void testLookup() {
+        final StrLookup lookup = new ResourceBundleLookup();
+        String value = lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle_en:KeyA");
+        Assert.assertEquals("ValueA", lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyA"));
+    }
+
+    @Test
+    public void testLookupWithLocale() {
+        final StrLookup lookup = new ResourceBundleLookup();
+        String value = lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyA");
+        Assert.assertEquals("ValueA", lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyA"));
+    }
+
+    public void testMissingKey() {
+        final StrLookup lookup = new ResourceBundleLookup();
+        Assert.assertNull(lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyUnkown"));
+    }
+
+    @Test
+    public void testBadFormatBundleOnly() {
+        final StrLookup lookup = new ResourceBundleLookup();
+        Assert.assertNull(lookup.lookup("X"));
+    }
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties?rev=1531064&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties Thu Oct 10 18:10:23 2013
@@ -0,0 +1 @@
+KeyA = ValueA

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle.properties
------------------------------------------------------------------------------
    svn:keywords = Id

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties?rev=1531064&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties Thu Oct 10 18:10:23 2013
@@ -0,0 +1 @@
+KeyA = ValueA

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/org/apache/logging/log4j/core/lookup/resource-bundle_en.properties
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1531064&r1=1531063&r2=1531064&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Thu Oct 10 18:10:23 2013
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0RC1" date="2013-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-420" dev="ggregory" type="add">
+        Create a lookup for resource bundle substitution.
+      </action>
       <action issue="LOG4J2-417" dev="ggregory" type="fix">
         Fix Event Level / LoggerConfig Level table at the architecture documentation page.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm?rev=1531064&r1=1531063&r2=1531064&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm Thu Oct 10 18:10:23 2013
@@ -804,6 +804,14 @@ public class Bar {
                 <th>Context</th>
               </tr>
               <tr>
+                <td>bundle</td>
+                <td>
+                  Resource bundle. The format is <code>${dollar}{bundle:BundleName:BundleKey}</code>.
+                  The bundle name follows package naming conventions, for example: 
+                  <code>${dollar}{bundle:com.domain.Messages:MyKey}</code>.
+                </td>
+              </tr>
+              <tr>
                 <td>ctx</td>
                 <td>Thread Context Map (MDC)</td>
               </tr>