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", type = "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(Map<String, String> map, Mode mode) {
49 this.map = map;
50 this.mode = mode;
51 }
52
53
54
55
56
57
58
59 public LogEvent rewrite(LogEvent source) {
60 Message msg = source.getMessage();
61 if (msg == null || !(msg instanceof MapMessage)) {
62 return source;
63 }
64
65 Map<String, String> newMap = new HashMap<String, String>(((MapMessage) msg).getData());
66 switch (mode) {
67 case Add: {
68 newMap.putAll(map);
69 break;
70 }
71 default: {
72 for (Map.Entry<String, String> entry : map.entrySet()) {
73 if (newMap.containsKey(entry.getKey())) {
74 newMap.put(entry.getKey(), entry.getValue());
75 }
76 }
77 }
78 }
79 MapMessage message = ((MapMessage) msg).newInstance(newMap);
80 return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getFQCN(), source.getLevel(),
81 message, source.getThrown(), source.getContextMap(), source.getContextStack(), source.getThreadName(),
82 source.getSource(), source.getMillis());
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 StringBuilder sb = new StringBuilder();
103 sb.append("mode=").append(mode);
104 sb.append(" {");
105 boolean first = true;
106 for (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(@PluginAttr("mode") String mode,
125 @PluginElement("KeyValuePair") KeyValuePair[] pairs) {
126 Mode op;
127 if (mode == null) {
128 op = Mode.Add;
129 } else {
130 op = Mode.valueOf(mode);
131 if (op == null) {
132 LOGGER.error("Undefined mode " + mode);
133 return null;
134 }
135 }
136 if (pairs == null || pairs.length == 0) {
137 LOGGER.error("keys and values must be specified for the MapRewritePolicy");
138 return null;
139 }
140 Map<String, String> map = new HashMap<String, String>();
141 for (KeyValuePair pair : pairs) {
142 String key = pair.getKey();
143 if (key == null) {
144 LOGGER.error("A null key is not valid in MapRewritePolicy");
145 continue;
146 }
147 String value = pair.getValue();
148 if (value == null) {
149 LOGGER.error("A null value for key " + key + " is not allowed in MapRewritePolicy");
150 continue;
151 }
152 map.put(pair.getKey(), pair.getValue());
153 }
154 if (map.size() == 0) {
155 LOGGER.error("MapRewritePolicy is not configured with any valid key value pairs");
156 return null;
157 }
158 return new MapRewritePolicy(map, op);
159 }
160 }