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.plugins.Plugin;
22 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
23 import org.apache.logging.log4j.core.config.plugins.PluginElement;
24 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25 import org.apache.logging.log4j.core.helpers.KeyValuePair;
26 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
27 import org.apache.logging.log4j.message.MapMessage;
28 import org.apache.logging.log4j.message.Message;
29 import org.apache.logging.log4j.status.StatusLogger;
30
31 import java.util.HashMap;
32 import java.util.Map;
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<String, String>(((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 return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getFQCN(), source.getLevel(),
82 message, source.getThrown(), source.getContextMap(), source.getContextStack(), source.getThreadName(),
83 source.getSource(), source.getMillis());
84 }
85
86
87
88
89
90 public enum Mode {
91
92
93
94 Add,
95
96
97
98 Update
99 }
100
101 @Override
102 public String toString() {
103 final StringBuilder sb = new StringBuilder();
104 sb.append("mode=").append(mode);
105 sb.append(" {");
106 boolean first = true;
107 for (final Map.Entry<String, String> entry : map.entrySet()) {
108 if (!first) {
109 sb.append(", ");
110 }
111 sb.append(entry.getKey()).append("=").append(entry.getValue());
112 first = false;
113 }
114 sb.append("}");
115 return sb.toString();
116 }
117
118
119
120
121
122
123
124 @PluginFactory
125 public static MapRewritePolicy createPolicy(@PluginAttr("mode") final String mode,
126 @PluginElement("KeyValuePair") final KeyValuePair[] pairs) {
127 Mode op;
128 if (mode == null) {
129 op = Mode.Add;
130 } else {
131 op = Mode.valueOf(mode);
132 if (op == null) {
133 LOGGER.error("Undefined mode " + mode);
134 return null;
135 }
136 }
137 if (pairs == null || pairs.length == 0) {
138 LOGGER.error("keys and values must be specified for the MapRewritePolicy");
139 return null;
140 }
141 final Map<String, String> map = new HashMap<String, String>();
142 for (final KeyValuePair pair : pairs) {
143 final String key = pair.getKey();
144 if (key == null) {
145 LOGGER.error("A null key is not valid in MapRewritePolicy");
146 continue;
147 }
148 final String value = pair.getValue();
149 if (value == null) {
150 LOGGER.error("A null value for key " + key + " is not allowed in MapRewritePolicy");
151 continue;
152 }
153 map.put(pair.getKey(), pair.getValue());
154 }
155 if (map.size() == 0) {
156 LOGGER.error("MapRewritePolicy is not configured with any valid key value pairs");
157 return null;
158 }
159 return new MapRewritePolicy(map, op);
160 }
161 }