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.LogEvent;
26  import org.apache.logging.log4j.core.config.Configuration;
27  import org.apache.logging.log4j.core.config.Property;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
30  import org.apache.logging.log4j.core.config.plugins.PluginElement;
31  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
32  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
33  import org.apache.logging.log4j.status.StatusLogger;
34  
35  /**
36   * This policy modifies events by replacing or possibly adding keys and values to the MapMessage.
37   */
38  @Plugin(name = "PropertiesRewritePolicy", category = "Core", elementType = "rewritePolicy", printObject = true)
39  public final class PropertiesRewritePolicy implements RewritePolicy {
40      /**
41       * Allow subclasses access to the status logger without creating another instance.
42       */
43      protected static final Logger LOGGER = StatusLogger.getLogger();
44  
45      private final Map<Property, Boolean> properties;
46  
47      private final Configuration config;
48  
49      private PropertiesRewritePolicy(final Configuration config, final List<Property> props) {
50          this.config = config;
51          this.properties = new HashMap<Property, Boolean>(props.size());
52          for (final Property property : props) {
53              final Boolean interpolate = Boolean.valueOf(property.getValue().contains("${"));
54              properties.put(property, interpolate);
55          }
56      }
57  
58      /**
59       * Rewrite the event.
60       * @param source a logging event that may be returned or
61       * used to create a new logging event.
62       * @return The LogEvent after rewriting.
63       */
64      @Override
65      public LogEvent rewrite(final LogEvent source) {
66          final Map<String, String> props = new HashMap<String, String>(source.getContextMap());
67          for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
68              final Property prop = entry.getKey();
69              props.put(prop.getName(), entry.getValue().booleanValue() ?
70                  config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue());
71          }
72  
73          return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getLoggerFqcn(), source.getLevel(),
74              source.getMessage(), source.getThrown(), props, source.getContextStack(), source.getThreadName(),
75              source.getSource(), source.getTimeMillis());
76      }
77  
78      @Override
79      public String toString() {
80          final StringBuilder sb = new StringBuilder();
81          sb.append(" {");
82          boolean first = true;
83          for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
84              if (!first) {
85                  sb.append(", ");
86              }
87              final Property prop = entry.getKey();
88              sb.append(prop.getName()).append('=').append(prop.getValue());
89              first = false;
90          }
91          sb.append('}');
92          return sb.toString();
93      }
94  
95      /**
96       * The factory method to create the PropertiesRewritePolicy.
97       * @param config The Configuration.
98       * @param props key/value pairs for the new keys and values.
99       * @return The PropertiesRewritePolicy.
100      */
101     @PluginFactory
102     public static PropertiesRewritePolicy createPolicy(@PluginConfiguration final Configuration config,
103                                                 @PluginElement("Properties") final Property[] props) {
104         if (props == null || props.length == 0) {
105             LOGGER.error("Properties must be specified for the PropertiesRewritePolicy");
106             return null;
107         }
108         final List<Property> properties = Arrays.asList(props);
109         return new PropertiesRewritePolicy(config, properties);
110     }
111 }