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