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 org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.core.LogEvent;
21 import org.apache.logging.log4j.core.config.Configuration;
22 import org.apache.logging.log4j.core.config.Property;
23 import org.apache.logging.log4j.core.config.plugins.Plugin;
24 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
25 import org.apache.logging.log4j.core.config.plugins.PluginElement;
26 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
28 import org.apache.logging.log4j.status.StatusLogger;
29
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38 @Plugin(name = "PropertiesRewritePolicy", type = "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(Configuration config, List<Property> props) {
50 this.config = config;
51 this.properties = new HashMap<Property, Boolean>(props.size());
52 for (Property prop : props) {
53 boolean interpolate = prop.getValue().contains("${");
54 properties.put(prop, interpolate);
55 }
56 }
57
58
59
60
61
62
63
64 public LogEvent rewrite(LogEvent source) {
65 Map<String, String> props = new HashMap<String, String>(source.getContextMap());
66 for (Map.Entry<Property, Boolean> entry : properties.entrySet()) {
67 Property prop = entry.getKey();
68 if (!props.containsKey(prop.getName())) {
69 props.put(prop.getName(), entry.getValue() ?
70 config.getSubst().replace(prop.getValue()) : prop.getValue());
71 }
72 }
73
74 return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getFQCN(), source.getLevel(),
75 source.getMessage(), source.getThrown(), props, source.getContextStack(), source.getThreadName(),
76 source.getSource(), source.getMillis());
77 }
78
79 @Override
80 public String toString() {
81 StringBuilder sb = new StringBuilder();
82 sb.append(" {");
83 boolean first = true;
84 for (Map.Entry<Property, Boolean> entry : properties.entrySet()) {
85 if (!first) {
86 sb.append(", ");
87 }
88 Property prop = entry.getKey();
89 sb.append(prop.getName()).append("=").append(prop.getValue());
90 first = false;
91 }
92 sb.append("}");
93 return sb.toString();
94 }
95
96
97
98
99
100
101
102 @PluginFactory
103 public static PropertiesRewritePolicy createPolicy(@PluginConfiguration Configuration config,
104 @PluginElement("properties") Property[] props) {
105 if (props == null || props.length == 0) {
106 LOGGER.error("Properties must be specified for the PropertiesRewritePolicy");
107 return null;
108 }
109 List<Property> properties = Arrays.asList(props);
110 return new PropertiesRewritePolicy(config, properties);
111 }
112 }