Search in sources :

Example 1 with EventResolverInterceptor

use of org.apache.logging.log4j.layout.template.json.resolver.EventResolverInterceptor in project logging-log4j2 by apache.

the class JsonTemplateLayout method createEventResolver.

private TemplateResolver<LogEvent> createEventResolver(final Builder builder, final Configuration configuration, final Charset charset, final JsonWriter jsonWriter) {
    // Inject resolver factory and interceptor plugins.
    final List<String> pluginPackages = configuration.getPluginPackages();
    final Map<String, EventResolverFactory> resolverFactoryByName = EventResolverFactories.populateResolverFactoryByName(pluginPackages);
    final List<EventResolverInterceptor> resolverInterceptors = EventResolverInterceptors.populateInterceptors(pluginPackages);
    final EventResolverStringSubstitutor substitutor = new EventResolverStringSubstitutor(configuration.getStrSubstitutor());
    // Read event and stack trace element templates.
    final String eventTemplate = readEventTemplate(builder);
    final String stackTraceElementTemplate = readStackTraceElementTemplate(builder);
    // Determine the max. string byte count.
    final float maxByteCountPerChar = builder.charset.newEncoder().maxBytesPerChar();
    final int maxStringByteCount = Math.toIntExact(Math.round(Math.ceil(maxByteCountPerChar * builder.maxStringLength)));
    // Replace null event template additional fields with an empty array.
    final EventTemplateAdditionalField[] eventTemplateAdditionalFields = builder.eventTemplateAdditionalFields != null ? builder.eventTemplateAdditionalFields : new EventTemplateAdditionalField[0];
    // Create the resolver context.
    final EventResolverContext resolverContext = EventResolverContext.newBuilder().setConfiguration(configuration).setResolverFactoryByName(resolverFactoryByName).setResolverInterceptors(resolverInterceptors).setSubstitutor(substitutor).setCharset(charset).setJsonWriter(jsonWriter).setRecyclerFactory(builder.recyclerFactory).setMaxStringByteCount(maxStringByteCount).setTruncatedStringSuffix(builder.truncatedStringSuffix).setLocationInfoEnabled(builder.locationInfoEnabled).setStackTraceEnabled(builder.stackTraceEnabled).setStackTraceElementTemplate(stackTraceElementTemplate).setEventTemplateRootObjectKey(builder.eventTemplateRootObjectKey).setEventTemplateAdditionalFields(eventTemplateAdditionalFields).build();
    // Compile the resolver template.
    return TemplateResolvers.ofTemplate(resolverContext, eventTemplate);
}
Also used : EventResolverStringSubstitutor(org.apache.logging.log4j.layout.template.json.resolver.EventResolverStringSubstitutor) EventResolverFactory(org.apache.logging.log4j.layout.template.json.resolver.EventResolverFactory) EventResolverContext(org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext) EventResolverInterceptor(org.apache.logging.log4j.layout.template.json.resolver.EventResolverInterceptor)

Aggregations

EventResolverContext (org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext)1 EventResolverFactory (org.apache.logging.log4j.layout.template.json.resolver.EventResolverFactory)1 EventResolverInterceptor (org.apache.logging.log4j.layout.template.json.resolver.EventResolverInterceptor)1 EventResolverStringSubstitutor (org.apache.logging.log4j.layout.template.json.resolver.EventResolverStringSubstitutor)1