Search in sources :

Example 1 with JsonWriter

use of org.apache.logging.log4j.layout.template.json.util.JsonWriter in project logging-log4j2 by apache.

the class JsonTemplateLayout method encode.

@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
    // Acquire a context.
    final Context context = acquireContext();
    final JsonWriter jsonWriter = context.jsonWriter;
    final StringBuilder stringBuilder = jsonWriter.getStringBuilder();
    final Encoder<StringBuilder> encoder = context.encoder;
    try {
        // Render the JSON.
        eventResolver.resolve(event, jsonWriter);
        stringBuilder.append(eventDelimiter);
        // Write to the destination.
        if (encoder == null) {
            final String eventJson = stringBuilder.toString();
            final byte[] eventJsonBytes = StringEncoder.toBytes(eventJson, charset);
            destination.writeBytes(eventJsonBytes, 0, eventJsonBytes.length);
        } else {
            encoder.encode(stringBuilder, destination);
        }
    } finally // Release the context.
    {
        contextRecycler.release(context);
    }
}
Also used : EventResolverContext(org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext) JsonWriter(org.apache.logging.log4j.layout.template.json.util.JsonWriter)

Example 2 with JsonWriter

use of org.apache.logging.log4j.layout.template.json.util.JsonWriter in project logging-log4j2 by apache.

the class CounterResolver method createStringifiedLongResolver.

private static Consumer<JsonWriter> createStringifiedLongResolver(final BigInteger start, final Recycler<StringBuilder> stringBuilderRecycler) {
    final long effectiveStart = start.longValue();
    final AtomicLong counter = new AtomicLong(effectiveStart);
    return (jsonWriter) -> {
        final long number = counter.getAndIncrement();
        final StringBuilder stringBuilder = stringBuilderRecycler.acquire();
        try {
            stringBuilder.append(number);
            jsonWriter.writeString(stringBuilder);
        } finally {
            stringBuilderRecycler.release(stringBuilder);
        }
    };
}
Also used : LockSupport(java.util.concurrent.locks.LockSupport) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) BigInteger(java.math.BigInteger) Recycler(org.apache.logging.log4j.layout.template.json.util.Recycler) LogEvent(org.apache.logging.log4j.core.LogEvent) JsonWriter(org.apache.logging.log4j.layout.template.json.util.JsonWriter) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 3 with JsonWriter

use of org.apache.logging.log4j.layout.template.json.util.JsonWriter in project logging-log4j2 by apache.

the class LevelResolver method createResolver.

private static EventResolver createResolver(final EventResolverContext context, final TemplateResolverConfig config) {
    final JsonWriter jsonWriter = context.getJsonWriter();
    final String fieldName = config.getString("field");
    if ("name".equals(fieldName)) {
        return createNameResolver(jsonWriter);
    } else if ("severity".equals(fieldName)) {
        final String severityFieldName = config.getString(new String[] { "severity", "field" });
        if ("keyword".equals(severityFieldName)) {
            return createSeverityKeywordResolver(jsonWriter);
        } else if ("code".equals(severityFieldName)) {
            return SEVERITY_CODE_RESOLVER;
        }
        throw new IllegalArgumentException("unknown severity field: " + config);
    }
    throw new IllegalArgumentException("unknown field: " + config);
}
Also used : JsonWriter(org.apache.logging.log4j.layout.template.json.util.JsonWriter)

Example 4 with JsonWriter

use of org.apache.logging.log4j.layout.template.json.util.JsonWriter in project logging-log4j2 by apache.

the class TemplateResolvers method ofString.

private static <V, C extends TemplateResolverContext<V, C>> TemplateResolver<V> ofString(final C context, final String fieldValue) {
    // Check if substitution is needed.
    final boolean substitutionNeeded = fieldValue.contains("${");
    final JsonWriter contextJsonWriter = context.getJsonWriter();
    if (substitutionNeeded) {
        final TemplateResolverStringSubstitutor<V> substitutor = context.getSubstitutor();
        // away and avoid runtime substitution.
        if (substitutor.isStable()) {
            final String replacedText = substitutor.replace(null, fieldValue);
            if (replacedText == null) {
                @SuppressWarnings("unchecked") final TemplateResolver<V> resolver = (TemplateResolver<V>) NULL_RESOLVER;
                return resolver;
            } else {
                // Prepare the escaped replacement first.
                final String escapedReplacedText = contextJsonWriter.use(() -> contextJsonWriter.writeString(replacedText));
                // Create a resolver dedicated to the escaped replacement.
                return (final V value, final JsonWriter jsonWriter) -> jsonWriter.writeRawString(escapedReplacedText);
            }
        } else // Otherwise, the unstable substitutor needs to be invoked always at
        // runtime.
        {
            return (final V value, final JsonWriter jsonWriter) -> {
                final String replacedText = substitutor.replace(value, fieldValue);
                jsonWriter.writeString(replacedText);
            };
        }
    } else // Write the field value as is.
    {
        final String escapedFieldValue = contextJsonWriter.use(() -> contextJsonWriter.writeString(fieldValue));
        return (final V value, final JsonWriter jsonWriter) -> jsonWriter.writeRawString(escapedFieldValue);
    }
}
Also used : JsonWriter(org.apache.logging.log4j.layout.template.json.util.JsonWriter)

Example 5 with JsonWriter

use of org.apache.logging.log4j.layout.template.json.util.JsonWriter in project logging-log4j2 by apache.

the class ReadOnlyStringMapResolver method createKeyResolver.

private static EventResolver createKeyResolver(final String key, final boolean stringified, final Function<LogEvent, ReadOnlyStringMap> mapAccessor) {
    return new EventResolver() {

        @Override
        public boolean isResolvable(final LogEvent logEvent) {
            final ReadOnlyStringMap map = mapAccessor.apply(logEvent);
            return map != null && map.containsKey(key);
        }

        @Override
        public void resolve(final LogEvent logEvent, final JsonWriter jsonWriter) {
            final ReadOnlyStringMap map = mapAccessor.apply(logEvent);
            final Object value = map == null ? null : map.getValue(key);
            if (stringified) {
                final String valueString = String.valueOf(value);
                jsonWriter.writeString(valueString);
            } else {
                jsonWriter.writeValue(value);
            }
        }
    };
}
Also used : ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) LogEvent(org.apache.logging.log4j.core.LogEvent) JsonWriter(org.apache.logging.log4j.layout.template.json.util.JsonWriter)

Aggregations

JsonWriter (org.apache.logging.log4j.layout.template.json.util.JsonWriter)8 LogEvent (org.apache.logging.log4j.core.LogEvent)4 BigInteger (java.math.BigInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 LockSupport (java.util.concurrent.locks.LockSupport)2 Consumer (java.util.function.Consumer)2 EventResolverContext (org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext)2 Recycler (org.apache.logging.log4j.layout.template.json.util.Recycler)2 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)2