1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
40
41 @Plugin(name = "PropertiesRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
42 public final class PropertiesRewritePolicy implements RewritePolicy {
43
44
45
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
64
65
66
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
99
100
101
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 }