View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rewrite;
18  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.core.Core;
26  import org.apache.logging.log4j.core.LogEvent;
27  import org.apache.logging.log4j.core.config.Configuration;
28  import org.apache.logging.log4j.core.config.Property;
29  import org.apache.logging.log4j.core.config.plugins.Plugin;
30  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
31  import org.apache.logging.log4j.core.config.plugins.PluginElement;
32  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
33  import org.apache.logging.log4j.core.impl.ContextDataFactory;
34  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
35  import org.apache.logging.log4j.status.StatusLogger;
36  import org.apache.logging.log4j.util.StringMap;
37  
38  /**
39   * This policy modifies events by replacing or possibly adding keys and values to the MapMessage.
40   */
41  @Plugin(name = "PropertiesRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
42  public final class PropertiesRewritePolicy implements RewritePolicy {
43  
44      /**
45       * Allows subclasses access to the status logger without creating another instance.
46       */
47      protected static final Logger LOGGER = StatusLogger.getLogger();
48  
49      private final Map<Property, Boolean> properties;
50  
51      private final Configuration config;
52  
53      private PropertiesRewritePolicy(final Configuration config, final List<Property> props) {
54          this.config = config;
55          this.properties = new HashMap<>(props.size());
56          for (final Property property : props) {
57              final Boolean interpolate = Boolean.valueOf(property.getValue().contains("${"));
58              properties.put(property, interpolate);
59          }
60      }
61  
62      /**
63       * Rewrites the event.
64       * @param source a logging event that may be returned or
65       * used to create a new logging event.
66       * @return The LogEvent after rewriting.
67       */
68      @Override
69      public LogEvent rewrite(final LogEvent source) {
70          final StringMap newContextData = ContextDataFactory.createContextData(source.getContextData());
71          for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
72              final Property prop = entry.getKey();
73              newContextData.putValue(prop.getName(), entry.getValue().booleanValue() ?
74                  config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue());
75          }
76  
77          return new Log4jLogEvent.Builder(source).setContextData(newContextData).build();
78      }
79  
80      @Override
81      public String toString() {
82          final StringBuilder sb = new StringBuilder();
83          sb.append(" {");
84          boolean first = true;
85          for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
86              if (!first) {
87                  sb.append(", ");
88              }
89              final Property prop = entry.getKey();
90              sb.append(prop.getName()).append('=').append(prop.getValue());
91              first = false;
92          }
93          sb.append('}');
94          return sb.toString();
95      }
96  
97      /**
98       * Creates a PropertiesRewritePolicy.
99       * @param config The Configuration.
100      * @param props key/value pairs for the new keys and values.
101      * @return The PropertiesRewritePolicy.
102      */
103     @PluginFactory
104     public static PropertiesRewritePolicy createPolicy(@PluginConfiguration final Configuration config,
105                                                 @PluginElement("Properties") final Property[] props) {
106         if (props == null || props.length == 0) {
107             LOGGER.error("Properties must be specified for the PropertiesRewritePolicy");
108             return null;
109         }
110         final List<Property> properties = Arrays.asList(props);
111         return new PropertiesRewritePolicy(config, properties);
112     }
113 }