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();
}
}
};
}
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);
}
}
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);
};
}
Aggregations