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.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
37
38 @Plugin(name = "PropertiesRewritePolicy", category = "Core", elementType = "rewritePolicy", printObject = true)
39 public final class PropertiesRewritePolicy implements RewritePolicy {
40
41
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<>(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
60
61
62
63
64 @Override
65 public LogEvent rewrite(final LogEvent source) {
66 final Map<String, String> props = new HashMap<>(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 final LogEvent result = new Log4jLogEvent.Builder(source).setContextMap(props).build();
74 return result;
75 }
76
77 @Override
78 public String toString() {
79 final StringBuilder sb = new StringBuilder();
80 sb.append(" {");
81 boolean first = true;
82 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
83 if (!first) {
84 sb.append(", ");
85 }
86 final Property prop = entry.getKey();
87 sb.append(prop.getName()).append('=').append(prop.getValue());
88 first = false;
89 }
90 sb.append('}');
91 return sb.toString();
92 }
93
94
95
96
97
98
99
100 @PluginFactory
101 public static PropertiesRewritePolicy createPolicy(@PluginConfiguration final Configuration config,
102 @PluginElement("Properties") final Property[] props) {
103 if (props == null || props.length == 0) {
104 LOGGER.error("Properties must be specified for the PropertiesRewritePolicy");
105 return null;
106 }
107 final List<Property> properties = Arrays.asList(props);
108 return new PropertiesRewritePolicy(config, properties);
109 }
110 }