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 F3F3395F1 for ; Thu, 13 Oct 2011 18:13:30 +0000 (UTC) Received: (qmail 41975 invoked by uid 500); 13 Oct 2011 18:13:28 -0000 Delivered-To: apmail-logging-commits-archive@logging.apache.org Received: (qmail 41848 invoked by uid 500); 13 Oct 2011 18:13:28 -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 41743 invoked by uid 99); 13 Oct 2011 18:13:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Oct 2011 18:13:28 +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, 13 Oct 2011 18:13:23 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 88C6323888CD; Thu, 13 Oct 2011 18:13:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1183017 [1/2] - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers: log4j2-api/src/main/java/org/apache/logging/log4j/ log4j2-api/src/test/java/org/apache/logging/log4j/ log4j2-core/src/main/java/org/apache/logging/log4j/core/append... Date: Thu, 13 Oct 2011 18:13:02 -0000 To: commits@logging.apache.org From: rgoers@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111013181303.88C6323888CD@eris.apache.org> Author: rgoers Date: Thu Oct 13 18:13:00 2011 New Revision: 1183017 URL: http://svn.apache.org/viewvc?rev=1183017&view=rev Log: Add tests for StrLookups and provide StrLookups access to the LogEvent. Add RoutingAppender Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginConfiguration.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginNode.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java - copied, changed from r1147720, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/routing/ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java 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/resources/log4j-routing.xml - copied, changed from r1147720, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-rolling1.xml Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/EventLogger.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/EventLoggerTest.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/AppenderControl.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/Plugin.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginType.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.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/lookup/MapLookup.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java 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/test/java/org/apache/logging/log4j/core/pattern/ExtendedThrowablePatternConverterTest.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/slf4j/helpers/Log4JMDCAdapter.java Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/EventLogger.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/EventLogger.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/EventLogger.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/EventLogger.java Thu Oct 13 18:13:00 2011 @@ -44,7 +44,7 @@ public class EventLogger { * @param msg The event StructuredDataMessage. */ public static void logEvent(StructuredDataMessage msg) { - logger.log(marker, FQCN, Level.ALL, msg, null); + logger.log(marker, FQCN, Level.OFF, msg, null); } /** Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/EventLoggerTest.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/EventLoggerTest.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/EventLoggerTest.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/EventLoggerTest.java Thu Oct 13 18:13:00 2011 @@ -51,7 +51,7 @@ public class EventLoggerTest { EventLogger.logEvent(msg); ThreadContext.clear(); assertEquals(1, results.size()); - String expected = " ALL Audit [Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"] Transfer Complete"; + String expected = " OFF Audit [Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"] Transfer Complete"; assertTrue("Incorrect structured data: " + results.get(0) + ", expected: " ,results.get(0).startsWith(expected)); } Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.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/routing/Route.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,95 @@ +/* + * 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.appender.routing; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.config.Node; +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.PluginFactory; +import org.apache.logging.log4j.core.config.plugins.PluginNode; +import org.apache.logging.log4j.status.StatusLogger; + +import java.util.List; + +/** + * + */ +@Plugin(name="Route", type="Core", printObject=true, deferChildren=true) +public class Route { + + private final Node node; + private final String appenderRef; + private final String key; + private static final Logger logger = StatusLogger.getLogger(); + + private Route(Node node, String appenderRef, String key) { + this.node = node; + this.appenderRef = appenderRef; + this.key = key; + } + + public Node getNode() { + return node; + } + + public String getAppenderRef() { + return appenderRef; + } + + public String getKey() { + return key; + } + + public String toString() { + StringBuilder sb = new StringBuilder("Route("); + sb.append("type="); + if (appenderRef != null) { + sb.append("static Reference=").append(appenderRef); + } else { + sb.append("dynamic"); + } + if (key != null) { + sb.append(" key='").append(key).append("'"); + } else { + sb.append(" default"); + } + sb.append(")"); + return sb.toString(); + } + + @PluginFactory + public static Route createRoute(@PluginAttr("appender-ref") String appenderRef, + @PluginAttr("key") String key, + @PluginNode Node node) { + if (node != null && node.hasChildren()) { + for (Node child : node.getChildren()) { + + } + if (appenderRef != null) { + logger.error("A route cannot be configured with an appender reference and an appender definition"); + return null; + } + } else { + if (appenderRef == null) { + logger.error("A route must specify an appender reference or an appender definition"); + return null; + } + } + return new Route(node, appenderRef, key); + } +} Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.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/routing/Routes.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/Routes.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,79 @@ +/* + * 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.appender.routing; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.config.Node; +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.PluginElement; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.core.config.plugins.PluginNode; +import org.apache.logging.log4j.status.StatusLogger; + +/** + * + */ +@Plugin(name="Routes", type="Core", printObject=true) +public class Routes { + + private final String pattern; + private final Route[] routes; + private static final Logger logger = StatusLogger.getLogger(); + + private Routes(String pattern, Route[] routes) { + this.pattern = pattern; + this.routes = routes; + } + + public String getPattern() { + return pattern; + } + + public Route[] getRoutes() { + return routes; + } + + public String toString() { + StringBuilder sb = new StringBuilder("Routes={"); + boolean first = true; + for (Route route : routes) { + if ((!first)) { + sb.append(","); + } + first = false; + sb.append(route.toString()); + } + sb.append("}"); + return sb.toString(); + + } + + @PluginFactory + public static Routes createRoutes(@PluginAttr("pattern") String pattern, + @PluginElement("routes") Route[] routes) { + if (pattern == null) { + logger.error("A pattern is required"); + return null; + } + if (routes == null || routes.length == 0) { + logger.error("No routes configured"); + return null; + } + return new Routes(pattern, routes); + } +} Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.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/routing/RoutingAppender.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,185 @@ +/* + * 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.appender.routing; + +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.appender.AppenderBase; +import org.apache.logging.log4j.core.config.AppenderControl; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.Node; +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.filter.Filters; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * This Appender "routes" between various Appenders, some of which can be references to + * Appenders defined earlier in the configuration while others can be dynamically created + * within this Appender as required. Routing is achieved by specifying a pattern on + * the Routing appender declaration. The pattern should contain one or more substitution patterns of + * the form "$${[key:]token}". The pattern will be resolved each time the Appender is called using + * the built in StrSubstitutor and the StrLookup plugin that matches the specified key. + */ +@Plugin(name="Routing",type="Core",elementType="appender",printObject=true) +public class RoutingAppender extends AppenderBase { + private static final String DEFAULT_KEY = "ROUTING_APPENDER_DEFAULT"; + private final Routes routes; + private final Configuration config; + private ConcurrentMap appenders = new ConcurrentHashMap(); + + private RoutingAppender(String name, Filters filters, boolean handleException, Routes routes, + Configuration config) { + super(name, filters, null, handleException); + this.routes = routes; + this.config = config; + } + + @Override + public void start() { + Map map = config.getAppenders(); + for (Route route : routes.getRoutes()) { + if (route.getAppenderRef() != null) { + Appender appender = map.get(route.getAppenderRef()); + if (appender != null) { + String key = route.getKey() == null ? DEFAULT_KEY : route.getKey(); + if (appenders.containsKey(key)) { + if (DEFAULT_KEY.equals(key)) { + logger.error("Multiple default routes. Only the first will be used"); + } else { + logger.error("Duplicate route " + key + " is ignored"); + } + } else { + appenders.put(key, new AppenderControl(appender)); + } + } else { + logger.error("Appender " + route.getAppenderRef() + " cannot be located. Route ignored"); + } + } + } + super.start(); + } + + @Override + public void stop() { + super.stop(); + for (AppenderControl control : appenders.values()) { + if (control instanceof AppenderWrapper) { + control.getAppender().stop(); + } + } + } + + public void append(LogEvent event) { + String key = config.getSubst().replace(event, routes.getPattern()); + AppenderControl control = getControl(key, event); + if (control != null) { + control.callAppender(event); + } + } + + private synchronized AppenderControl getControl(String key, LogEvent event) { + AppenderControl control = appenders.get(key); + boolean defaultRoute = false; + if (control != null) { + return control; + } + Route route = null; + for (Route r : routes.getRoutes()) { + if (r.getAppenderRef() == null && key.equals(r.getKey())) { + route = r; + break; + } + } + if (route == null) { + control = appenders.get(DEFAULT_KEY); + if (control != null) { + return control; + } + for (Route r : routes.getRoutes()) { + if (r.getAppenderRef() == null && r.getKey() == null) { + route = r; + defaultRoute = true; + break; + } + } + } + if (route != null) { + Appender app = createAppender(route, event); + if (app == null) { + return null; + } + control = new AppenderWrapper(app); + appenders.put(key, control); + if (defaultRoute) { + appenders.put(DEFAULT_KEY, control); + } + } + + return control; + } + + private Appender createAppender(Route route, LogEvent event) { + Node routeNode = route.getNode(); + for (Node node : routeNode.getChildren()) { + if (node.getType().getElementName().equals("appender")) { + config.createConfiguration(node, event); + if (node.getObject() instanceof Appender) { + Appender app = (Appender) node.getObject(); + app.start(); + return (Appender) node.getObject(); + } + logger.error("Unable to create Appender of type " + node.getName()); + return null; + } + } + logger.error("No Appender was configured for route " + route.getKey()); + return null; + } + + @PluginFactory + public static RoutingAppender createAppender(@PluginAttr("name") String name, + @PluginAttr("suppressExceptions") String suppress, + @PluginElement("routes") Routes routes, + @PluginConfiguration Configuration config, + @PluginElement("filters") Filters filters) { + + boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); + + if (name == null) { + logger.error("No name provided for RoutingAppender"); + return null; + } + if (routes == null) { + logger.error("No routes defined for RoutingAppender"); + return null; + } + return new RoutingAppender(name, filters, handleExceptions, routes, config); + } + + private static class AppenderWrapper extends AppenderControl { + public AppenderWrapper(Appender appender) { + super(appender); + } + } +} Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/AppenderControl.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/AppenderControl.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/AppenderControl.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/AppenderControl.java Thu Oct 13 18:13:00 2011 @@ -40,7 +40,7 @@ public class AppenderControl { * Constructor. * @param appender The target Appender. */ - AppenderControl(Appender appender) { + public AppenderControl(Appender appender) { this.appender = appender; } 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=1183017&r1=1183016&r2=1183017&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 Thu Oct 13 18:13:00 2011 @@ -18,14 +18,14 @@ package org.apache.logging.log4j.core.co import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.ErrorHandler; import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.Lifecycle; -import org.apache.logging.log4j.core.appender.DefaultErrorHandler; +import org.apache.logging.log4j.core.LogEvent; 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.config.plugins.PluginManager; import org.apache.logging.log4j.core.config.plugins.PluginElement; +import org.apache.logging.log4j.core.config.plugins.PluginNode; import org.apache.logging.log4j.core.config.plugins.PluginType; import org.apache.logging.log4j.core.config.plugins.PluginValue; import org.apache.logging.log4j.core.filter.Filterable; @@ -116,7 +116,7 @@ public class BaseConfiguration extends F boolean setRoot = false; boolean setLoggers = false; for (Node child : rootNode.getChildren()) { - createConfiguration(child); + createConfiguration(child, null); if (child.getObject() == null) { continue; } @@ -225,6 +225,10 @@ public class BaseConfiguration extends F appenders.put(appender.getName(), appender); } + public StrSubstitutor getSubst() { + return subst; + } + /** * Associates an Appender with a LoggerConfig. This method is synchronized in case a Logger with the * same name is being updated at the same time. @@ -302,6 +306,7 @@ public class BaseConfiguration extends F * Remove an Appender. First removes any associations between LoggerContigs and the Appender, removes * the Appender from this appender list and then stops the appender. This method is synchronized in * case an Appender with the same name is being added during the removal. + * @param name the name of the appender to remove. */ public synchronized void removeAppender(String name) { for (LoggerConfig logger : loggers.values()) { @@ -324,7 +329,6 @@ public class BaseConfiguration extends F if (loggers.containsKey(name)) { return loggers.get(name); } - int i = 0; String substr = name; while ((substr = NameUtil.getSubName(substr)) != null) { if (loggers.containsKey(substr)) { @@ -392,19 +396,73 @@ public class BaseConfiguration extends F setParents(); } - private void createConfiguration(Node node) { - for (Node child : node.getChildren()) { - createConfiguration(child); - } + public void createConfiguration(Node node, LogEvent event) { PluginType type = node.getType(); - if (type == null) { - if (node.getParent() != null) { - logger.error("Unable to locate plugin for " + node.getName()); - } + if (type != null && type.deferChildren()) { + node.setObject(createPluginObject(type, node, event)); } else { - node.setObject(createPluginObject(type, node)); + for (Node child : node.getChildren()) { + createConfiguration(child, event); + } + + if (type == null) { + if (node.getParent() != null) { + logger.error("Unable to locate plugin for " + node.getName()); + } + } else { + node.setObject(createPluginObject(type, node, event)); + } + } + } + + /** + * Create an Object that will contain its child Nodes and defer their construction until + * a later time. + * @param type The PluginType. + * @param node The Node. + * @return The constructed container object. + */ + private Object createPluginContainer(PluginType type, Node node) { + Class clazz = type.getPluginClass(); + Method factoryMethod = null; + + for (Method method : clazz.getMethods()) { + if (method.isAnnotationPresent(PluginFactory.class)) { + factoryMethod = method; + break; + } + } + if (factoryMethod == null) { + return null; + } + Class[] parmClasses = factoryMethod.getParameterTypes(); + if (parmClasses.length != 1 && Node.class.equals(parmClasses[0])) { + logger.error("Template factory method must contain a single Node parameter"); + } + + try + { + int mod = factoryMethod.getModifiers(); + if (!Modifier.isStatic(mod)) + { + logger.error(factoryMethod.getName() + " method is not static on class " + + clazz.getName() + " for element " + node.getName()); + return null; + } + logger.debug("Calling " + factoryMethod.getName() + " on class " + clazz.getName() + " for element " + + node.getName()); + + return factoryMethod.invoke(null, node); } + catch (Exception e) + { + logger.error("Unable to invoke method " + factoryMethod.getName() + " in class " + + clazz.getName() + " for element " + node.getName(), e); + } + return null; + } + /* * Retrieve a static public 'method to create the desired object. Every parameter * will be annotated to identify the appropriate attribute or element to use to @@ -420,7 +478,7 @@ public class BaseConfiguration extends F * @return the instantiate method or null if there is none by that * description. */ - public Object createPluginObject(PluginType type, Node node) + private Object createPluginObject(PluginType type, Node node, LogEvent event) { Class clazz = type.getPluginClass(); @@ -463,7 +521,7 @@ public class BaseConfiguration extends F Annotation[][] parmArray = factoryMethod.getParameterAnnotations(); Class[] parmClasses = factoryMethod.getParameterTypes(); if (parmArray.length != parmClasses.length) { - logger.error(""); + logger.error("Number of parameter annotations does not equal the number of paramters"); } Object[] parms = new Object[parmClasses.length]; @@ -490,15 +548,24 @@ public class BaseConfiguration extends F } else { sb.append(", "); } - if (a instanceof PluginValue) { + if (a instanceof PluginNode) { + parms[index] = node; + sb.append("Node=").append(node.getName()); + } else if (a instanceof PluginConfiguration) { + parms[index] = this; + if (this.name != null) { + sb.append("Configuration(").append(name).append(")"); + } else { + sb.append("Configuration"); + } + } else if (a instanceof PluginValue) { String name = ((PluginValue)a).value(); - String value = subst.replace(node.getValue()); + String value = subst.replace(event, node.getValue()); sb.append(name +"=" + "\"" + value + "\""); parms[index] = value; - } - if (a instanceof PluginAttr) { + } else if (a instanceof PluginAttr) { String name = ((PluginAttr)a).value(); - String value = subst.replace(getAttrValue(name, attrs)); + String value = subst.replace(event, getAttrValue(name, attrs)); sb.append(name +"=" + "\"" + value + "\""); parms[index] = value; } else if (a instanceof PluginElement) { @@ -519,6 +586,9 @@ public class BaseConfiguration extends F } first = false; Object obj = child.getObject(); + if (obj == null) { + System.out.println("Null object returned for " + child.getName()); + } if (obj.getClass().isArray()) { printArray(sb, (Object[])obj); parms[index] = obj; @@ -587,7 +657,7 @@ public class BaseConfiguration extends F logger.error(eb.toString()); } - if (used.size() != children.size()) { + if (!type.deferChildren() && used.size() != children.size()) { for (Node child : children) { if (used.contains(child)) { continue; @@ -608,7 +678,6 @@ public class BaseConfiguration extends F } logger.debug("Calling " + factoryMethod.getName() + " on class " + clazz.getName() + " for element " + node.getName() + sb.toString()); - StringBuilder b = new StringBuilder(); //if (parms.length > 0) { return factoryMethod.invoke(null, parms); //} @@ -629,6 +698,7 @@ public class BaseConfiguration extends F sb.append(", "); } sb.append(obj.toString()); + first = false; } } 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=1183017&r1=1183016&r2=1183017&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 Thu Oct 13 18:13:00 2011 @@ -2,8 +2,10 @@ package org.apache.logging.log4j.core.co import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.filter.Filtering; +import org.apache.logging.log4j.core.lookup.StrSubstitutor; import java.util.Iterator; import java.util.List; @@ -50,4 +52,7 @@ public interface Configuration extends F void removeListener(ConfigurationListener listener); + StrSubstitutor getSubst(); + + void createConfiguration(Node node, LogEvent event); } Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.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/Plugin.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java Thu Oct 13 18:13:00 2011 @@ -16,6 +16,7 @@ public @interface Plugin { public String type(); public String elementType() default NULL; public boolean printObject() default false; + public boolean deferChildren() default false; public static final String NULL = ""; } Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginConfiguration.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/PluginConfiguration.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginConfiguration.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginConfiguration.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,14 @@ +package org.apache.logging.log4j.core.config.plugins; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface PluginConfiguration { +} Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.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/PluginManager.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java Thu Oct 13 18:13:00 2011 @@ -79,7 +79,7 @@ public class PluginManager { { Plugin p = item.getAnnotation(Plugin.class); String type = p.elementType().equals(Plugin.NULL) ? p.name() : p.elementType(); - plugins.put(p.name(), new PluginType(item, type, p.printObject())); + plugins.put(p.name(), new PluginType(item, type, p.printObject(), p.deferChildren())); } } Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginNode.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/PluginNode.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginNode.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginNode.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,14 @@ +package org.apache.logging.log4j.core.config.plugins; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface PluginNode { +} Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginType.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/PluginType.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginType.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginType.java Thu Oct 13 18:13:00 2011 @@ -18,18 +18,20 @@ package org.apache.logging.log4j.core.co /** - * + * Plugin Descriptor. */ public class PluginType { private Class pluginClass; private String elementName; private boolean printObject = false; + private boolean deferChildren = false; - public PluginType(Class clazz, String name, boolean printObj) { + public PluginType(Class clazz, String name, boolean printObj, boolean deferChildren) { this.pluginClass = clazz; this.elementName = name; this.printObject = printObj; + this.deferChildren = deferChildren; } public Class getPluginClass() { @@ -44,4 +46,8 @@ public class PluginType { return this.printObject; } + public boolean deferChildren() { + return this.deferChildren; + } + } Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.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/ContextMapLookup.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java Thu Oct 13 18:13:00 2011 @@ -17,6 +17,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; /** @@ -26,6 +27,10 @@ import org.apache.logging.log4j.core.con public class ContextMapLookup implements StrLookup { public String lookup(String key) { - return ThreadContext.get(key).toString(); + return ThreadContext.get(key); + } + + public String lookup(LogEvent event, String key) { + return ThreadContext.get(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=1183017&r1=1183016&r2=1183017&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 Thu Oct 13 18:13:00 2011 @@ -17,6 +17,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LogEvent; 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; @@ -43,7 +44,6 @@ public class Interpolator implements Str PluginManager manager = new PluginManager("Lookup"); manager.collectPlugins(); Map plugins = manager.getPlugins(); - Map lookups = new HashMap(); for (Map.Entry entry : plugins.entrySet()) { Class clazz = entry.getValue().getPluginClass(); @@ -69,6 +69,24 @@ public class Interpolator implements Str */ public String lookup(String var) { + return lookup(null, var); + } + + /** + * Resolves the specified variable. This implementation will try to extract + * a variable prefix from the given variable name (the first colon (':') is + * used as prefix separator). It then passes the name of the variable with + * the prefix stripped to the lookup object registered for this prefix. If + * no prefix can be found or if the associated lookup object cannot resolve + * this variable, the default lookup object will be used. + * + * @param event The current LogEvent or null. + * @param var the name of the variable whose value is to be looked up + * @return the value of this variable or null if it cannot be + * resolved + */ + public String lookup(LogEvent event, String var) + { if (var == null) { return null; @@ -82,7 +100,7 @@ public class Interpolator implements Str StrLookup lookup = lookups.get(prefix); String value = null; if (lookup != null) { - value = lookup.lookup(name); + value = event == null ? lookup.lookup(name) : lookup.lookup(event, name); } if (value != null) @@ -92,7 +110,7 @@ public class Interpolator implements Str var = var.substring(prefixPos); } if (defaultLookup != null) { - return defaultLookup.lookup(var); + return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var); } return null; } Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.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/MapLookup.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java Thu Oct 13 18:13:00 2011 @@ -16,6 +16,8 @@ */ package org.apache.logging.log4j.core.lookup; +import org.apache.logging.log4j.core.LogEvent; + import java.util.Map; /** @@ -55,4 +57,8 @@ public class MapLookup implements Str } return obj.toString(); } + + public String lookup(LogEvent event, String key) { + return lookup(key); + } } Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.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/StrLookup.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java Thu Oct 13 18:13:00 2011 @@ -16,6 +16,8 @@ */ package org.apache.logging.log4j.core.lookup; +import org.apache.logging.log4j.core.LogEvent; + /** * Lookup a String key to a String value. *

@@ -57,4 +59,30 @@ public interface StrLookup { * @return the matching value, null if no match */ public String lookup(String key); + + /** + * Looks up a String key to a String value possibly using the current LogEvent. + *

+ * The internal implementation may use any mechanism to return the value. + * The simplest implementation is to use a Map. However, virtually any + * implementation is possible. + *

+ * For example, it would be possible to implement a lookup that used the + * key as a primary key, and looked up the value on demand from the database + * Or, a numeric based implementation could be created that treats the key + * as an integer, increments the value and return the result as a string - + * converting 1 to 2, 15 to 16 etc. + *

+ * The {@link #lookup(String)} method always returns a String, regardless of + * the underlying data, by converting it as necessary. For example: + *

+     * Map map = new HashMap();
+     * map.put("number", new Integer(2));
+     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
+     * 
+ * @param event The current LogEvent. + * @param key the key to be looked up, may be null + * @return the matching value, null if no match + */ + public String lookup(LogEvent event, String key); } \ No newline at end of file Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.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/StrSubstitutor.java?rev=1183017&r1=1183016&r2=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java Thu Oct 13 18:13:00 2011 @@ -16,6 +16,8 @@ */ package org.apache.logging.log4j.core.lookup; +import org.apache.logging.log4j.core.LogEvent; + import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -286,11 +288,23 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(String source) { + return replace(null, source); + } + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source string as a template. + * + * @param event The current LogEvent if there is one. + * @param source the string to replace in, null returns null + * @return the result of the replace operation + */ + public String replace(LogEvent event, String source) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(source); - if (substitute(buf, 0, source.length()) == false) { + if (substitute(event, buf, 0, source.length()) == false) { return source; } return buf.toString(); @@ -309,11 +323,28 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(String source, int offset, int length) { + return replace(null, source, offset, length); + } + + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source string as a template. + *

+ * Only the specified portion of the string will be processed. + * The rest of the string is not processed, and is not returned. + * + * @param event the current LogEvent, if one exists. + * @param source the string to replace in, null returns null + * @param offset the start offset within the array, must be valid + * @param length the length within the array to be processed, must be valid + * @return the result of the replace operation + */ + public String replace(LogEvent event, String source, int offset, int length) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(length).append(source, offset, length); - if (substitute(buf, 0, length) == false) { + if (substitute(event, buf, 0, length) == false) { return source.substring(offset, offset + length); } return buf.toString(); @@ -329,11 +360,25 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(char[] source) { + return replace(null, source); + } + + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source array as a template. + * The array is not altered by this method. + * + * @param event the current LogEvent, if one exists. + * @param source the character array to replace in, not altered, null returns null + * @return the result of the replace operation + */ + public String replace(LogEvent event, char[] source) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(source.length).append(source); - substitute(buf, 0, source.length); + substitute(event, buf, 0, source.length); return buf.toString(); } @@ -351,11 +396,29 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(char[] source, int offset, int length) { + return replace(null, source, offset, length); + } + + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source array as a template. + * The array is not altered by this method. + *

+ * Only the specified portion of the array will be processed. + * The rest of the array is not processed, and is not returned. + * + * @param event the current LogEvent, if one exists. + * @param source the character array to replace in, not altered, null returns null + * @param offset the start offset within the array, must be valid + * @param length the length within the array to be processed, must be valid + * @return the result of the replace operation + */ + public String replace(LogEvent event, char[] source, int offset, int length) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(length).append(source, offset, length); - substitute(buf, 0, length); + substitute(event, buf, 0, length); return buf.toString(); } @@ -369,11 +432,25 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(StringBuffer source) { + return replace(null, source); + } + + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source buffer as a template. + * The buffer is not altered by this method. + * + * @param event the current LogEvent, if one exists. + * @param source the buffer to use as a template, not changed, null returns null + * @return the result of the replace operation + */ + public String replace(LogEvent event, StringBuffer source) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(source.length()).append(source); - substitute(buf, 0, buf.length()); + substitute(event, buf, 0, buf.length()); return buf.toString(); } @@ -391,11 +468,29 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(StringBuffer source, int offset, int length) { + return replace(null, source, offset, length); + } + + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source buffer as a template. + * The buffer is not altered by this method. + *

+ * Only the specified portion of the buffer will be processed. + * The rest of the buffer is not processed, and is not returned. + * + * @param event the current LogEvent, if one exists. + * @param source the buffer to use as a template, not changed, null returns null + * @param offset the start offset within the array, must be valid + * @param length the length within the array to be processed, must be valid + * @return the result of the replace operation + */ + public String replace(LogEvent event, StringBuffer source, int offset, int length) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(length).append(source, offset, length); - substitute(buf, 0, length); + substitute(event, buf, 0, length); return buf.toString(); } @@ -409,14 +504,26 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(StringBuilder source) { + return replace(null, source); + } + + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source builder as a template. + * The builder is not altered by this method. + * + * @param source the builder to use as a template, not changed, null returns null + * @return the result of the replace operation + */ + public String replace(LogEvent event, StringBuilder source) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(source.length()).append(source); - substitute(buf, 0, buf.length()); + substitute(event, buf, 0, buf.length()); return buf.toString(); } - /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source builder as a template. @@ -431,11 +538,29 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(StringBuilder source, int offset, int length) { + return replace(null, source, offset, length); + } + + /** + * Replaces all the occurrences of variables with their matching values + * from the resolver using the given source builder as a template. + * The builder is not altered by this method. + *

+ * Only the specified portion of the builder will be processed. + * The rest of the builder is not processed, and is not returned. + * + * @param event the current LogEvent, if one exists. + * @param source the builder to use as a template, not changed, null returns null + * @param offset the start offset within the array, must be valid + * @param length the length within the array to be processed, must be valid + * @return the result of the replace operation + */ + public String replace(LogEvent event, StringBuilder source, int offset, int length) { if (source == null) { return null; } StringBuilder buf = new StringBuilder(length).append(source, offset, length); - substitute(buf, 0, length); + substitute(event, buf, 0, length); return buf.toString(); } @@ -449,11 +574,24 @@ public class StrSubstitutor { * @return the result of the replace operation */ public String replace(Object source) { + return replace(null, source); + } + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables in the given source object with + * their matching values from the resolver. The input source object is + * converted to a string using toString and is not altered. + * + * @param event the current LogEvent, if one exists. + * @param source the source to replace in, null returns null + * @return the result of the replace operation + */ + public String replace(LogEvent event, Object source) { if (source == null) { return null; } StringBuilder buf = new StringBuilder().append(source); - substitute(buf, 0, buf.length()); + substitute(event, buf, 0, buf.length()); return buf.toString(); } @@ -487,11 +625,29 @@ public class StrSubstitutor { * @return true if altered */ public boolean replaceIn(StringBuffer source, int offset, int length) { + return replaceIn(null, source, offset, length); + } + + /** + * Replaces all the occurrences of variables within the given source buffer + * with their matching values from the resolver. + * The buffer is updated with the result. + *

+ * Only the specified portion of the buffer will be processed. + * The rest of the buffer is not processed, but it is not deleted. + * + * @param event the current LogEvent, if one exists. + * @param source the buffer to replace in, updated, null returns zero + * @param offset the start offset within the array, must be valid + * @param length the length within the buffer to be processed, must be valid + * @return true if altered + */ + public boolean replaceIn(LogEvent event, StringBuffer source, int offset, int length) { if (source == null) { return false; } StringBuilder buf = new StringBuilder(length).append(source, offset, length); - if (substitute(buf, 0, length) == false) { + if (substitute(event, buf, 0, length) == false) { return false; } source.replace(offset, offset + length, buf.toString()); @@ -507,12 +663,24 @@ public class StrSubstitutor { * @return true if altered */ public boolean replaceIn(StringBuilder source) { + return replaceIn(null, source); + } + + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables within the given source + * builder with their matching values from the resolver. + * + * @param event the current LogEvent, if one exists. + * @param source the builder to replace in, updated, null returns zero + * @return true if altered + */ + public boolean replaceIn(LogEvent event, StringBuilder source) { if (source == null) { return false; } - return substitute(source, 0, source.length()); + return substitute(event, source, 0, source.length()); } - /** * Replaces all the occurrences of variables within the given source * builder with their matching values from the resolver. @@ -526,10 +694,27 @@ public class StrSubstitutor { * @return true if altered */ public boolean replaceIn(StringBuilder source, int offset, int length) { + return replaceIn(null, source, offset, length); + } + + /** + * Replaces all the occurrences of variables within the given source + * builder with their matching values from the resolver. + *

+ * Only the specified portion of the builder will be processed. + * The rest of the builder is not processed, but it is not deleted. + * + * @param event the current LogEvent, if one is present. + * @param source the builder to replace in, null returns zero + * @param offset the start offset within the array, must be valid + * @param length the length within the builder to be processed, must be valid + * @return true if altered + */ + public boolean replaceIn(LogEvent event, StringBuilder source, int offset, int length) { if (source == null) { return false; } - return substitute(source, offset, length); + return substitute(event, source, offset, length); } //----------------------------------------------------------------------- @@ -542,13 +727,14 @@ public class StrSubstitutor { * Writers of subclasses can override this method if they need access to * the substitution process at the start or end. * + * @param event The current LogEvent, if there is one. * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ - protected boolean substitute(StringBuilder buf, int offset, int length) { - return substitute(buf, offset, length, null) > 0; + protected boolean substitute(LogEvent event, StringBuilder buf, int offset, int length) { + return substitute(event, buf, offset, length, null) > 0; } /** @@ -556,6 +742,7 @@ public class StrSubstitutor { * interpolation method, which resolves the values of all variable references * contained in the passed in text. * + * @param event The current LogEvent, if there is one. * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid * @param length the length within the builder to be processed, must be valid @@ -563,7 +750,7 @@ public class StrSubstitutor { * @return the length change that occurs, unless priorVariables is null when the int * represents a boolean flag as to whether any change occurred. */ - private int substitute(StringBuilder buf, int offset, int length, List priorVariables) { + private int substitute(LogEvent event, StringBuilder buf, int offset, int length, List priorVariables) { StrMatcher prefixMatcher = getVariablePrefixMatcher(); StrMatcher suffixMatcher = getVariableSuffixMatcher(); char escape = getEscapeChar(); @@ -616,7 +803,7 @@ public class StrSubstitutor { - startMatchLen); if (isEnableSubstitutionInVariables()) { StringBuilder bufName = new StringBuilder(varName); - substitute(bufName, 0, bufName.length()); + substitute(event, bufName, 0, bufName.length()); varName = bufName.toString(); } pos += endMatchLen; @@ -634,14 +821,14 @@ public class StrSubstitutor { priorVariables.add(varName); // resolve the variable - String varValue = resolveVariable(varName, buf, + String varValue = resolveVariable(event, varName, buf, startPos, endPos); if (varValue != null) { // recursive replace int varLen = varValue.length(); buf.replace(startPos, endPos, varValue); altered = true; - int change = substitute(buf, startPos, + int change = substitute(event, buf, startPos, varLen, priorVariables); change = change + (varLen - (endPos - startPos)); @@ -700,18 +887,20 @@ public class StrSubstitutor { * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * + * @param event The LogEvent, if there is one. * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or null if the variable is unknown */ - protected String resolveVariable(String variableName, StringBuilder buf, int startPos, int endPos) { + protected String resolveVariable(LogEvent event, String variableName, StringBuilder buf, int startPos, + int endPos) { StrLookup resolver = getVariableResolver(); if (resolver == null) { return null; } - return resolver.lookup(variableName); + return resolver.lookup(event, variableName); } // Escape Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java (from r1147720, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.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/StructuredDataLookup.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java&r1=1147720&r2=1183017&rev=1183017&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java Thu Oct 13 18:13:00 2011 @@ -17,15 +17,30 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.message.StructuredDataMessage; /** * Looks up keys from system properties */ -@Plugin(name="ctx",type="Lookup") -public class ContextMapLookup implements StrLookup { +@Plugin(name="sd",type="Lookup") +public class StructuredDataLookup implements StrLookup { public String lookup(String key) { - return ThreadContext.get(key).toString(); + return null; + } + + public String lookup(LogEvent event, String key) { + if (event == null || !(event.getMessage() instanceof StructuredDataMessage)) { + return null; + } + StructuredDataMessage msg = (StructuredDataMessage) event.getMessage(); + if (key.equalsIgnoreCase("id")) { + return msg.getId().getName(); + } else if (key.equalsIgnoreCase("type")) { + return msg.getType(); + } + return msg.get(key); } } Modified: 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/SystemPropertiesLookup.java?rev=1183017&r1=1183016&r2=1183017&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/SystemPropertiesLookup.java Thu Oct 13 18:13:00 2011 @@ -16,6 +16,7 @@ */ package org.apache.logging.log4j.core.lookup; +import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; /** @@ -27,4 +28,8 @@ public class SystemPropertiesLookup impl public String lookup(String key) { return System.getProperty(key); } + + public String lookup(LogEvent event, String key) { + return System.getProperty(key); + } } Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.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/appender/routing/RoutingAppenderTest.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,86 @@ +/* + * 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.appender.routing; + +import org.apache.logging.log4j.EventLogger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +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.message.StructuredDataMessage; +import org.apache.logging.log4j.status.StatusLogger; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +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 RoutingAppenderTest { + private static final String CONFIG = "log4j-routing.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 entry : config.getAppenders().entrySet()) { + if (entry.getKey().equals("List")) { + app = (ListAppender) entry.getValue(); + break; + } + } + File file = new File("target/rolling1/rollingtest-Unknown.log"); + file.delete(); + } + + @AfterClass + public static void cleanupClass() { + System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + ctx.reconfigure(); + StatusLogger.getLogger().reset(); + File file = new File("target/rolling1/rollingtest-Unknown.log"); + file.delete(); + } + + @Test + public void routingTest() { + StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service"); + EventLogger.logEvent(msg); + List list = app.getEvents(); + assertNotNull("No events generated", list); + assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1); + msg = new StructuredDataMessage("Test", "This is a test", "Unknown"); + EventLogger.logEvent(msg); + File file = new File("target/rolling1/rollingtest-Unknown.log"); + assertTrue("File was not created", file.exists()); + } +} Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.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/ContextMapLookupTest.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,42 @@ +/* + * 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.apache.logging.log4j.ThreadContext; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * + */ +public class ContextMapLookupTest { + + private static final String TESTKEY = "TestKey"; + private static final String TESTVAL = "TestValue"; + + @Test + public void testLookup() { + ThreadContext.put(TESTKEY, TESTVAL); + StrLookup lookup = new ContextMapLookup(); + String value = lookup.lookup(TESTKEY); + assertEquals(TESTVAL, value); + value = lookup.lookup("BadKey"); + assertNull(value); + } +} Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.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/InterpolatorTest.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,65 @@ +/* + * 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.apache.logging.log4j.ThreadContext; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * + */ +public class InterpolatorTest { + + 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() { + Map map = new HashMap(); + map.put(TESTKEY, TESTVAL); + StrLookup lookup = new Interpolator(new MapLookup(map)); + ThreadContext.put(TESTKEY, TESTVAL); + String value = lookup.lookup(TESTKEY); + assertEquals(TESTVAL, value); + value = lookup.lookup("ctx:" + TESTKEY); + assertEquals(TESTVAL, value); + value = lookup.lookup("sys:" + TESTKEY); + assertEquals(TESTVAL, value); + value = lookup.lookup("BadKey"); + assertNull(value); + } +} Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.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/StrSubstitutorTest.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,62 @@ +/* + * 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.apache.logging.log4j.ThreadContext; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * + */ +public class StrSubstitutorTest { + + 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() { + Map map = new HashMap(); + map.put(TESTKEY, TESTVAL); + StrLookup lookup = new Interpolator(new MapLookup(map)); + StrSubstitutor subst = new StrSubstitutor(lookup); + ThreadContext.put(TESTKEY, TESTVAL); + String value = subst.replace("${TestKey}-${ctx:TestKey}-${sys:TestKey}"); + assertEquals("TestValue-TestValue-TestValue", value); + value = subst.replace("${BadKey}"); + assertEquals("${BadKey}", value); + } +} Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.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/StructuredDataLookupTest.java?rev=1183017&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java Thu Oct 13 18:13:00 2011 @@ -0,0 +1,49 @@ +/* + * 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.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.Message; +import org.apache.logging.log4j.message.StructuredDataMessage; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * + */ +public class StructuredDataLookupTest { + + private static final String TESTKEY = "type"; + private static final String TESTVAL = "Audit"; + + @Test + public void testLookup() { + Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); + LogEvent event = new Log4jLogEvent(null, null, null, Level.DEBUG, msg, null); + StrLookup lookup = new StructuredDataLookup(); + String value = lookup.lookup(event, TESTKEY); + assertEquals(TESTVAL, value); + value = lookup.lookup("BadKey"); + assertNull(value); + } +}