Return-Path: X-Original-To: apmail-logging-commits-archive@minotaur.apache.org Delivered-To: apmail-logging-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 80A60100C1 for ; Thu, 10 Oct 2013 18:10:57 +0000 (UTC) Received: (qmail 34773 invoked by uid 500); 10 Oct 2013 18:10:55 -0000 Delivered-To: apmail-logging-commits-archive@logging.apache.org Received: (qmail 34481 invoked by uid 500); 10 Oct 2013 18:10:51 -0000 Mailing-List: contact commits-help@logging.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@logging.apache.org Delivered-To: mailing list commits@logging.apache.org Received: (qmail 34045 invoked by uid 99); 10 Oct 2013 18:10:49 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Oct 2013 18:10:49 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Oct 2013 18:10:45 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 4E3322388A56; Thu, 10 Oct 2013 18:10:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Thu, 10 Oct 2013 18:10:24 -0000 To: commits@logging.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131010181024.4E3322388A56@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ + + Create a lookup for resource bundle substitution. + Fix Event Level / LoggerConfig Level table at the architecture documentation page. 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 { Context + bundle + + Resource bundle. The format is ${dollar}{bundle:BundleName:BundleKey}. + The bundle name follows package naming conventions, for example: + ${dollar}{bundle:com.domain.Messages:MyKey}. + + + ctx Thread Context Map (MDC)