|
| 1 | +package org.apache.logging.log4j.core.layout; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonAnyGetter; |
| 4 | +import com.fasterxml.jackson.annotation.JsonGetter; |
| 5 | +import com.fasterxml.jackson.annotation.JsonRootName; |
| 6 | +import com.fasterxml.jackson.annotation.JsonUnwrapped; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import org.apache.logging.log4j.core.Layout; |
| 9 | +import org.apache.logging.log4j.core.LogEvent; |
| 10 | +import org.apache.logging.log4j.core.config.Configuration; |
| 11 | +import org.apache.logging.log4j.core.config.DefaultConfiguration; |
| 12 | +import org.apache.logging.log4j.core.config.Node; |
| 13 | +import org.apache.logging.log4j.core.config.plugins.Plugin; |
| 14 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; |
| 15 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; |
| 16 | +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper; |
| 17 | +import org.apache.logging.log4j.core.jackson.XmlConstants; |
| 18 | +import org.apache.logging.log4j.core.util.KeyValuePair; |
| 19 | +import org.apache.logging.log4j.util.Strings; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.Writer; |
| 23 | +import java.nio.charset.Charset; |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | +import java.time.ZoneId; |
| 26 | +import java.time.ZonedDateTime; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static java.time.Instant.ofEpochMilli; |
| 32 | +import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME; |
| 33 | + |
| 34 | +@Plugin(name = "LambdaJsonLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) |
| 35 | +public class LambdaJsonLayout extends AbstractJacksonLayout { |
| 36 | + |
| 37 | + private static final String DEFAULT_FOOTER = "]"; |
| 38 | + |
| 39 | + private static final String DEFAULT_HEADER = "["; |
| 40 | + |
| 41 | + static final String CONTENT_TYPE = "application/json"; |
| 42 | + |
| 43 | + private ObjectMapper objectMapper; |
| 44 | + |
| 45 | + public static class Builder<B extends Builder<B>> extends AbstractJacksonLayout.Builder<B> |
| 46 | + implements org.apache.logging.log4j.core.util.Builder<LambdaJsonLayout> { |
| 47 | + |
| 48 | + @PluginBuilderAttribute |
| 49 | + private boolean propertiesAsList; |
| 50 | + |
| 51 | + @PluginBuilderAttribute |
| 52 | + private boolean objectMessageAsJsonObject; |
| 53 | + |
| 54 | + public Builder() { |
| 55 | + super(); |
| 56 | + setCharset(StandardCharsets.UTF_8); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public LambdaJsonLayout build() { |
| 61 | + final boolean encodeThreadContextAsList = isProperties() && propertiesAsList; |
| 62 | + final String headerPattern = toStringOrNull(getHeader()); |
| 63 | + final String footerPattern = toStringOrNull(getFooter()); |
| 64 | + return new LambdaJsonLayout(getConfiguration(), isLocationInfo(), isProperties(), encodeThreadContextAsList, |
| 65 | + isComplete(), isCompact(), getEventEol(), headerPattern, footerPattern, getCharset(), |
| 66 | + isIncludeStacktrace(), isStacktraceAsString(), isIncludeNullDelimiter(), |
| 67 | + getAdditionalFields(), getObjectMessageAsJsonObject()); |
| 68 | + } |
| 69 | + |
| 70 | + public boolean isPropertiesAsList() { |
| 71 | + return propertiesAsList; |
| 72 | + } |
| 73 | + |
| 74 | + public B setPropertiesAsList(final boolean propertiesAsList) { |
| 75 | + this.propertiesAsList = propertiesAsList; |
| 76 | + return asBuilder(); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean getObjectMessageAsJsonObject() { |
| 80 | + return objectMessageAsJsonObject; |
| 81 | + } |
| 82 | + |
| 83 | + public B setObjectMessageAsJsonObject(final boolean objectMessageAsJsonObject) { |
| 84 | + this.objectMessageAsJsonObject = objectMessageAsJsonObject; |
| 85 | + return asBuilder(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private LambdaJsonLayout(final Configuration config, final boolean locationInfo, final boolean properties, |
| 90 | + final boolean encodeThreadContextAsList, |
| 91 | + final boolean complete, final boolean compact, final boolean eventEol, |
| 92 | + final String headerPattern, final String footerPattern, final Charset charset, |
| 93 | + final boolean includeStacktrace, final boolean stacktraceAsString, |
| 94 | + final boolean includeNullDelimiter, |
| 95 | + final KeyValuePair[] additionalFields, final boolean objectMessageAsJsonObject) { |
| 96 | + super(config, new JacksonFactory.JSON(encodeThreadContextAsList, includeStacktrace, stacktraceAsString, objectMessageAsJsonObject).newWriter( |
| 97 | + locationInfo, properties, compact), |
| 98 | + charset, compact, complete, eventEol, |
| 99 | + null, |
| 100 | + PatternLayout.newSerializerBuilder().setConfiguration(config).setPattern(headerPattern).setDefaultPattern(DEFAULT_HEADER).build(), |
| 101 | + PatternLayout.newSerializerBuilder().setConfiguration(config).setPattern(footerPattern).setDefaultPattern(DEFAULT_FOOTER).build(), |
| 102 | + includeNullDelimiter, |
| 103 | + additionalFields); |
| 104 | + this.objectMapper = new Log4jJsonObjectMapper(encodeThreadContextAsList, includeStacktrace, stacktraceAsString, objectMessageAsJsonObject); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns appropriate JSON header. |
| 109 | + * |
| 110 | + * @return a byte array containing the header, opening the JSON array. |
| 111 | + */ |
| 112 | + @Override |
| 113 | + public byte[] getHeader() { |
| 114 | + if (!this.complete) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + final StringBuilder buf = new StringBuilder(); |
| 118 | + final String str = serializeToString(getHeaderSerializer()); |
| 119 | + if (str != null) { |
| 120 | + buf.append(str); |
| 121 | + } |
| 122 | + buf.append(this.eol); |
| 123 | + return getBytes(buf.toString()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns appropriate JSON footer. |
| 128 | + * |
| 129 | + * @return a byte array containing the footer, closing the JSON array. |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public byte[] getFooter() { |
| 133 | + if (!this.complete) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + final StringBuilder buf = new StringBuilder(); |
| 137 | + buf.append(this.eol); |
| 138 | + final String str = serializeToString(getFooterSerializer()); |
| 139 | + if (str != null) { |
| 140 | + buf.append(str); |
| 141 | + } |
| 142 | + buf.append(this.eol); |
| 143 | + return getBytes(buf.toString()); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Map<String, String> getContentFormat() { |
| 148 | + final Map<String, String> result = new HashMap<>(); |
| 149 | + result.put("version", "2.0"); |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @return The content type. |
| 155 | + */ |
| 156 | + @Override |
| 157 | + public String getContentType() { |
| 158 | + return CONTENT_TYPE + "; charset=" + this.getCharset(); |
| 159 | + } |
| 160 | + |
| 161 | + @PluginBuilderFactory |
| 162 | + public static <B extends Builder<B>> B newBuilder() { |
| 163 | + return new Builder<B>().asBuilder(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Creates a JSON Layout using the default settings. Useful for testing. |
| 168 | + * |
| 169 | + * @return A JSON Layout. |
| 170 | + */ |
| 171 | + public static LambdaJsonLayout createDefaultLayout() { |
| 172 | + return new LambdaJsonLayout(new DefaultConfiguration(), false, false, false, false, false, false, |
| 173 | + DEFAULT_HEADER, DEFAULT_FOOTER, StandardCharsets.UTF_8, true, false, false, null, false); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public Object wrapLogEvent(final LogEvent event) { |
| 178 | + Map<String, Object> additionalFieldsMap = resolveAdditionalFields(event); |
| 179 | + // This class combines LogEvent with AdditionalFields during serialization |
| 180 | + return new LogEventWithAdditionalFields(event, additionalFieldsMap); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void toSerializable(final LogEvent event, final Writer writer) throws IOException { |
| 185 | + if (complete && eventCount > 0) { |
| 186 | + writer.append(", "); |
| 187 | + } |
| 188 | + super.toSerializable(event, writer); |
| 189 | + } |
| 190 | + |
| 191 | + private Map<String, Object> resolveAdditionalFields(LogEvent logEvent) { |
| 192 | + // Note: LinkedHashMap retains order |
| 193 | + final Map<String, Object> additionalFieldsMap = new LinkedHashMap<>(additionalFields.length); |
| 194 | + |
| 195 | + // Go over MDC |
| 196 | + logEvent.getContextData().forEach((key, value) -> { |
| 197 | + if (Strings.isNotBlank(key) && value != null) { |
| 198 | + additionalFieldsMap.put(key, value); |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + return additionalFieldsMap; |
| 203 | + } |
| 204 | + |
| 205 | + @JsonRootName(XmlConstants.ELT_EVENT) |
| 206 | + public static class LogEventWithAdditionalFields { |
| 207 | + |
| 208 | + private final LogEvent logEvent; |
| 209 | + private final Map<String, Object> additionalFields; |
| 210 | + |
| 211 | + public LogEventWithAdditionalFields(LogEvent logEvent, Map<String, Object> additionalFields) { |
| 212 | + this.logEvent = logEvent; |
| 213 | + this.additionalFields = additionalFields; |
| 214 | + } |
| 215 | + |
| 216 | + @JsonUnwrapped |
| 217 | + public Object getLogEvent() { |
| 218 | + return logEvent; |
| 219 | + } |
| 220 | + |
| 221 | + @JsonAnyGetter |
| 222 | + public Map<String, Object> getAdditionalFields() { |
| 223 | + return additionalFields; |
| 224 | + } |
| 225 | + |
| 226 | + @JsonGetter("timestamp") |
| 227 | + public String getTimestamp() { |
| 228 | + return ISO_ZONED_DATE_TIME.format(ZonedDateTime.from(ofEpochMilli(logEvent.getTimeMillis()).atZone(ZoneId.systemDefault()))); |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments