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.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.logging.log4j.core.Layout;
25 import org.apache.logging.log4j.core.config.Configuration;
26 import org.apache.logging.log4j.core.config.Node;
27 import org.apache.logging.log4j.core.config.plugins.Plugin;
28 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
29 import org.apache.logging.log4j.core.jackson.XmlConstants;
30 import org.apache.logging.log4j.core.util.KeyValuePair;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Plugin(name = "XmlLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
63 public final class XmlLayout extends AbstractJacksonLayout {
64
65 private static final String ROOT_TAG = "Events";
66
67 public static class Builder<B extends Builder<B>> extends AbstractJacksonLayout.Builder<B>
68 implements org.apache.logging.log4j.core.util.Builder<XmlLayout> {
69
70 public Builder() {
71 super();
72 setCharset(StandardCharsets.UTF_8);
73 }
74
75 @Override
76 public XmlLayout build() {
77 return new XmlLayout(getConfiguration(), isLocationInfo(), isProperties(), isComplete(),
78 isCompact(), getEndOfLine(), getCharset(), isIncludeStacktrace(), isStacktraceAsString(),
79 isIncludeNullDelimiter(), getAdditionalFields());
80 }
81 }
82
83
84
85
86 @Deprecated
87 protected XmlLayout(final boolean locationInfo, final boolean properties, final boolean complete,
88 final boolean compact, final Charset charset, final boolean includeStacktrace) {
89 this(null, locationInfo, properties, complete, compact, null, charset, includeStacktrace, false, false, null);
90 }
91
92 private XmlLayout(final Configuration config, final boolean locationInfo, final boolean properties,
93 final boolean complete, final boolean compact, final String endOfLine, final Charset charset,
94 final boolean includeStacktrace, final boolean stacktraceAsString,
95 final boolean includeNullDelimiter,
96 final KeyValuePair[] additionalFields) {
97 super(config, new JacksonFactory.XML(includeStacktrace, stacktraceAsString).newWriter(
98 locationInfo, properties, compact),
99 charset, compact, complete, false, endOfLine, null, null, includeNullDelimiter,
100 additionalFields);
101 }
102
103
104
105
106
107
108
109
110
111
112 @Override
113 public byte[] getHeader() {
114 if (!complete) {
115 return null;
116 }
117 final StringBuilder buf = new StringBuilder();
118 buf.append("<?xml version=\"1.0\" encoding=\"");
119 buf.append(this.getCharset().name());
120 buf.append("\"?>");
121 buf.append(this.eol);
122
123 buf.append('<');
124 buf.append(ROOT_TAG);
125 buf.append(" xmlns=\"" + XmlConstants.XML_NAMESPACE + "\">");
126 buf.append(this.eol);
127 return buf.toString().getBytes(this.getCharset());
128 }
129
130
131
132
133
134
135 @Override
136 public byte[] getFooter() {
137 if (!complete) {
138 return null;
139 }
140 return getBytes("</" + ROOT_TAG + '>' + this.eol);
141 }
142
143
144
145
146
147
148
149
150
151
152 @Override
153 public Map<String, String> getContentFormat() {
154 final Map<String, String> result = new HashMap<>();
155
156 result.put("xsd", "log4j-events.xsd");
157 result.put("version", "2.0");
158 return result;
159 }
160
161
162
163
164 @Override
165 public String getContentType() {
166 return "text/xml; charset=" + this.getCharset();
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 @Deprecated
184 public static XmlLayout createLayout(
185 final boolean locationInfo,
186 final boolean properties,
187 final boolean complete,
188 final boolean compact,
189 final Charset charset,
190 final boolean includeStacktrace) {
191 return new XmlLayout(null, locationInfo, properties, complete, compact, null, charset, includeStacktrace, false,
192 false, null);
193 }
194
195 @PluginBuilderFactory
196 public static <B extends Builder<B>> B newBuilder() {
197 return new Builder<B>().asBuilder();
198 }
199
200
201
202
203
204
205 public static XmlLayout createDefaultLayout() {
206 return new XmlLayout(null, false, false, false, false, null, StandardCharsets.UTF_8, true, false, false, null);
207 }
208 }