All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.logging.json.EventJsonLayoutBaseFactory Maven / Gradle / Ivy

There is a newer version: 5.0.0-alpha.4
Show newest version
package io.dropwizard.logging.json;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.pattern.ExtendedThrowableProxyConverter;
import ch.qos.logback.classic.pattern.RootCauseFirstThrowableProxyConverter;
import ch.qos.logback.classic.pattern.ThrowableHandlingConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.LayoutBase;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.logging.json.layout.EventJsonLayout;
import io.dropwizard.logging.json.layout.ExceptionFormat;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
NameDefaultDescription
{@code includes}(level, threadName, mdc, loggerName, message, exception, timestamp, callerData)Set of logging event attributes to include in the JSON map.
{@code includesMdcKeys}(empty)Set of MDC keys which should be included in the JSON map. By default includes everything.
{@code flattenMdc}{@code false}Whether the MDC should be included under the key "mdc" or flattened into the map.
*/ @JsonTypeName("json") public class EventJsonLayoutBaseFactory extends AbstractJsonLayoutBaseFactory { private EnumSet includes = EnumSet.of(EventAttribute.LEVEL, EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.MARKER, EventAttribute.LOGGER_NAME, EventAttribute.MESSAGE, EventAttribute.EXCEPTION, EventAttribute.TIMESTAMP); private Set includesMdcKeys = Collections.emptySet(); private boolean flattenMdc = false; @Nullable private ExceptionFormat exceptionFormat; @JsonProperty public EnumSet getIncludes() { return includes; } @JsonProperty public void setIncludes(EnumSet includes) { this.includes = includes; } @JsonProperty public Set getIncludesMdcKeys() { return includesMdcKeys; } @JsonProperty public void setIncludesMdcKeys(Set includesMdcKeys) { this.includesMdcKeys = includesMdcKeys; } @JsonProperty public boolean isFlattenMdc() { return flattenMdc; } @JsonProperty public void setFlattenMdc(boolean flattenMdc) { this.flattenMdc = flattenMdc; } /** * @since 2.0 */ @JsonProperty("exception") public void setExceptionFormat(ExceptionFormat exceptionFormat) { this.exceptionFormat = exceptionFormat; } /** * @since 2.0 */ @JsonProperty("exception") @Nullable public ExceptionFormat getExceptionFormat() { return exceptionFormat; } @Override public LayoutBase build(LoggerContext context, TimeZone timeZone) { final EventJsonLayout jsonLayout = new EventJsonLayout(createDropwizardJsonFormatter(), createTimestampFormatter(timeZone), createThrowableProxyConverter(context), includes, getCustomFieldNames(), getAdditionalFields(), includesMdcKeys, flattenMdc); jsonLayout.setContext(context); return jsonLayout; } protected ThrowableHandlingConverter createThrowableProxyConverter(LoggerContext context) { if (exceptionFormat == null) { return new RootCauseFirstThrowableProxyConverter(); } ThrowableHandlingConverter throwableHandlingConverter; if (exceptionFormat.isRootFirst()) { throwableHandlingConverter = new RootCauseFirstThrowableProxyConverter(); } else { throwableHandlingConverter = new ExtendedThrowableProxyConverter(); } List options = new ArrayList<>(); // depth must be added first options.add(exceptionFormat.getDepth()); options.addAll(exceptionFormat.getEvaluators()); throwableHandlingConverter.setOptionList(options); throwableHandlingConverter.setContext(context); return throwableHandlingConverter; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy