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.HashMap;
20 import java.util.Map;
21
22 import org.apache.logging.log4j.Logger;
23 import org.apache.logging.log4j.core.LogEvent;
24 import org.apache.logging.log4j.core.config.plugins.Plugin;
25 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
26 import org.apache.logging.log4j.core.config.plugins.PluginElement;
27 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
29 import org.apache.logging.log4j.core.util.KeyValuePair;
30 import org.apache.logging.log4j.message.MapMessage;
31 import org.apache.logging.log4j.message.Message;
32 import org.apache.logging.log4j.status.StatusLogger;
33
34
35
36
37 @Plugin(name = "MapRewritePolicy", category = "Core", elementType = "rewritePolicy", printObject = true)
38 public final class MapRewritePolicy implements RewritePolicy {
39
40
41
42 protected static final Logger LOGGER = StatusLogger.getLogger();
43
44 private final Map<String, String> map;
45
46 private final Mode mode;
47
48 private MapRewritePolicy(final Map<String, String> map, final Mode mode) {
49 this.map = map;
50 this.mode = mode;
51 }
52
53
54
55
56
57
58
59 @Override
60 public LogEvent rewrite(final LogEvent source) {
61 final Message msg = source.getMessage();
62 if (msg == null || !(msg instanceof MapMessage)) {
63 return source;
64 }
65
66 final Map<String, String> newMap = new HashMap<>(((MapMessage) msg).getData());
67 switch (mode) {
68 case Add: {
69 newMap.putAll(map);
70 break;
71 }
72 default: {
73 for (final Map.Entry<String, String> entry : map.entrySet()) {
74 if (newMap.containsKey(entry.getKey())) {
75 newMap.put(entry.getKey(), entry.getValue());
76 }
77 }
78 }
79 }
80 final MapMessage message = ((MapMessage) msg).newInstance(newMap);
81 final LogEvent result = new Log4jLogEvent.Builder(source).setMessage(message).build();
82 return result;
83 }
84
85
86
87
88
89 public enum Mode {
90
91
92
93 Add,
94
95
96
97 Update
98 }
99
100 @Override
101 public String toString() {
102 final StringBuilder sb = new StringBuilder();
103 sb.append("mode=").append(mode);
104 sb.append(" {");
105 boolean first = true;
106 for (final Map.Entry<String, String> entry : map.entrySet()) {
107 if (!first) {
108 sb.append(", ");
109 }
110 sb.append(entry.getKey()).append('=').append(entry.getValue());
111 first = false;
112 }
113 sb.append('}');
114 return sb.toString();
115 }
116
117
118
119
120
121
122
123 @PluginFactory
124 public static MapRewritePolicy createPolicy(
125 @PluginAttribute("mode") final String mode,
126 @PluginElement("KeyValuePair") final KeyValuePair[] pairs) {
127 Mode op = mode == null ? op = Mode.Add : Mode.valueOf(mode);
128 if (pairs == null || pairs.length == 0) {
129 LOGGER.error("keys and values must be specified for the MapRewritePolicy");
130 return null;
131 }
132 final Map<String, String> map = new HashMap<>();
133 for (final KeyValuePair pair : pairs) {
134 final String key = pair.getKey();
135 if (key == null) {
136 LOGGER.error("A null key is not valid in MapRewritePolicy");
137 continue;
138 }
139 final String value = pair.getValue();
140 if (value == null) {
141 LOGGER.error("A null value for key " + key + " is not allowed in MapRewritePolicy");
142 continue;
143 }
144 map.put(pair.getKey(), pair.getValue());
145 }
146 if (map.isEmpty()) {
147 LOGGER.error("MapRewritePolicy is not configured with any valid key value pairs");
148 return null;
149 }
150 return new MapRewritePolicy(map, op);
151 }
152 }