Search in sources :

Example 6 with JsonWriter

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

the class ReadOnlyStringMapResolver method createResolver.

private static EventResolver createResolver(final boolean flatten, final Recycler<LoopContext> loopContextRecycler, final Function<LogEvent, ReadOnlyStringMap> mapAccessor) {
    return new EventResolver() {

        @Override
        public boolean isFlattening() {
            return flatten;
        }

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

        @Override
        public void resolve(final LogEvent value, final JsonWriter jsonWriter) {
            resolve(value, jsonWriter, false);
        }

        @Override
        public void resolve(final LogEvent logEvent, final JsonWriter jsonWriter, final boolean succeedingEntry) {
            // Retrieve the map.
            final ReadOnlyStringMap map = mapAccessor.apply(logEvent);
            if (map == null || map.isEmpty()) {
                if (!flatten) {
                    jsonWriter.writeNull();
                }
                return;
            }
            // Resolve the map.
            if (!flatten) {
                jsonWriter.writeObjectStart();
            }
            final LoopContext loopContext = loopContextRecycler.acquire();
            loopContext.jsonWriter = jsonWriter;
            loopContext.initJsonWriterStringBuilderLength = jsonWriter.getStringBuilder().length();
            loopContext.succeedingEntry = flatten && succeedingEntry;
            try {
                map.forEach(LoopMethod.INSTANCE, loopContext);
            } finally {
                loopContextRecycler.release(loopContext);
            }
            if (!flatten) {
                jsonWriter.writeObjectEnd();
            }
        }
    };
}
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)

Example 7 with JsonWriter

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

the class JsonTemplateLayout method toSerializable.

@Override
public String toSerializable(final LogEvent event) {
    final Context context = acquireContext();
    final JsonWriter jsonWriter = context.jsonWriter;
    final StringBuilder stringBuilder = jsonWriter.getStringBuilder();
    try {
        eventResolver.resolve(event, jsonWriter);
        stringBuilder.append(eventDelimiter);
        return stringBuilder.toString();
    } finally {
        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 8 with JsonWriter

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

the class CounterResolver method createLongResolver.

private static Consumer<JsonWriter> createLongResolver(final BigInteger start) {
    final long effectiveStart = start.longValue();
    final AtomicLong counter = new AtomicLong(effectiveStart);
    return (jsonWriter) -> {
        final long number = counter.getAndIncrement();
        jsonWriter.writeNumber(number);
    };
}
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)

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