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.Log4jLogEvent;
34 import org.apache.logging.log4j.status.StatusLogger;
35
36
37
38
39 @Plugin(name = "PropertiesRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
40 public final class PropertiesRewritePolicy implements RewritePolicy {
41
42
43
44 protected static final Logger LOGGER = StatusLogger.getLogger();
45
46 private final Map<Property, Boolean> properties;
47
48 private final Configuration config;
49
50 private PropertiesRewritePolicy(final Configuration config, final List<Property> props) {
51 this.config = config;
52 this.properties = new HashMap<>(props.size());
53 for (final Property property : props) {
54 final Boolean interpolate = Boolean.valueOf(property.getValue().contains("${"));
55 properties.put(property, interpolate);
56 }
57 }
58
59
60
61
62
63
64
65 @Override
66 public LogEvent rewrite(final LogEvent source) {
67 final Map<String, String> props = new HashMap<>(source.getContextData().toMap());
68 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
69 final Property prop = entry.getKey();
70 props.put(prop.getName(), entry.getValue().booleanValue() ?
71 config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue());
72 }
73
74 final LogEvent result = new Log4jLogEvent.Builder(source).setContextMap(props).build();
75 return result;
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
97
98
99
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 }