1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.layout;
18
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.logging.log4j.core.config.plugins.Plugin;
24 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
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.util.KeyValuePair;
28 import org.apache.logging.log4j.message.StructuredDataId;
29
30
31
32
33 @Plugin(name = "LoggerFields", category = "Core", printObject = true)
34 public final class LoggerFields {
35
36 private final Map<String, String> map;
37 private final String sdId;
38 private final String enterpriseId;
39 private final boolean discardIfAllFieldsAreEmpty;
40
41 private LoggerFields(final Map<String, String> map, final String sdId, final String enterpriseId,
42 final boolean discardIfAllFieldsAreEmpty) {
43 this.sdId = sdId;
44 this.enterpriseId = enterpriseId;
45 this.map = Collections.unmodifiableMap(map);
46 this.discardIfAllFieldsAreEmpty = discardIfAllFieldsAreEmpty;
47 }
48
49 public Map<String, String> getMap() {
50 return this.map;
51 }
52
53 @Override
54 public String toString() {
55 return this.map.toString();
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @PluginFactory
72 public static LoggerFields createLoggerFields(
73 @PluginElement("LoggerFields") final KeyValuePair[] keyValuePairs,
74 @PluginAttribute("sdId") final String sdId,
75 @PluginAttribute("enterpriseId") final String enterpriseId,
76 @PluginAttribute(value = "discardIfAllFieldsAreEmpty", defaultBoolean = false) final boolean discardIfAllFieldsAreEmpty) {
77 final Map<String, String> map = new HashMap<String, String>();
78
79 for (final KeyValuePair keyValuePair : keyValuePairs) {
80 map.put(keyValuePair.getKey(), keyValuePair.getValue());
81 }
82
83 return new LoggerFields(map, sdId, enterpriseId, discardIfAllFieldsAreEmpty);
84 }
85
86 public StructuredDataId getSdId() {
87 if (enterpriseId == null || sdId == null) {
88 return null;
89 }
90 final int eId = Integer.parseInt(enterpriseId);
91 return new StructuredDataId(sdId, eId, null, null);
92 }
93
94 public boolean getDiscardIfAllFieldsAreEmpty() {
95 return discardIfAllFieldsAreEmpty;
96 }
97 }