com.agido.logback.elasticsearch.PropertySerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logback-elasticsearch-appender Show documentation
Show all versions of logback-elasticsearch-appender Show documentation
Send log events directly from Logback to Elasticsearch
The newest version!
package com.agido.logback.elasticsearch;
import com.agido.logback.elasticsearch.util.AbstractPropertyAndEncoder;
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;
class PropertySerializer {
void serializeProperty(JsonGenerator jsonGenerator, T event, AbstractPropertyAndEncoder propertyAndEncoder) throws IOException {
String value = propertyAndEncoder.encode(event);
if (propertyAndEncoder.allowEmpty() || (value != null && !value.isEmpty())) {
switch (propertyAndEncoder.getType()) {
case INT:
serializeIntField(jsonGenerator, propertyAndEncoder, value);
break;
case FLOAT:
serializeFloatField(jsonGenerator, propertyAndEncoder, value);
break;
case BOOLEAN:
serializeBooleanField(jsonGenerator, propertyAndEncoder, value);
break;
default:
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
}
}
private void serializeStringField(JsonGenerator jsonGenerator, AbstractPropertyAndEncoder propertyAndEncoder, String value) throws IOException {
jsonGenerator.writeObjectField(propertyAndEncoder.getName(), value);
}
private void serializeIntField(JsonGenerator jsonGenerator, AbstractPropertyAndEncoder propertyAndEncoder, String value) throws IOException {
try {
jsonGenerator.writeNumberField(propertyAndEncoder.getName(), Integer.valueOf(value));
} catch (NumberFormatException e) {
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
}
private void serializeFloatField(JsonGenerator jsonGenerator, AbstractPropertyAndEncoder propertyAndEncoder, String value) throws IOException {
try {
jsonGenerator.writeNumberField(propertyAndEncoder.getName(), Float.valueOf(value));
} catch (NumberFormatException e) {
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
}
private void serializeBooleanField(JsonGenerator jsonGenerator, AbstractPropertyAndEncoder propertyAndEncoder, String value) throws IOException {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
jsonGenerator.writeBooleanField(propertyAndEncoder.getName(), Boolean.valueOf(value));
} else {
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
}
}