Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"object" data type for properties #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ In your `logback.xml`:
<appender name="ES_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- ... -->
<encoder>
<pattern>%msg</pattern> <!-- This pattern is important, otherwise it won't be the raw Elasticsearch format anyomre -->
<pattern>%msg</pattern> <!-- This pattern is important, otherwise it won't be the raw Elasticsearch format anymore -->
</encoder>
</appender>
</logger>
Expand Down Expand Up @@ -116,7 +116,7 @@ The fields `@timestamp` and `message` are always sent and can not currently be c
* `name` (required): Key to be used in the log event
* `value` (required): Text string to be sent. Internally, the value is populated using a Logback PatternLayout, so all [Conversion Words](http://logback.qos.ch/manual/layouts.html#conversionWord) can be used (in addition to the standard static variable interpolations like `${HOSTNAME}`).
* `allowEmpty` (optional, default `false`): Normally, if the `value` results in a `null` or empty string, the field will not be sent. If `allowEmpty` is set to `true` then the field will be sent regardless
* `type` (optional, default `String`): type of the field on the resulting JSON message. Possible values are: `String`, `int`, `float` and `boolean`.
* `type` (optional, default `String`): type of the field on the resulting JSON message. Possible values are: `String`, `int`, `float`, `boolean` and `object`. Use `object` if the value is the string representation of a JSON object or array ie. `{"k" : true}`or `[1,2,3,]`.

Groovy Configuration
====================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ void serializeProperty(JsonGenerator jsonGenerator, T event, AbstractPropertyAnd
case BOOLEAN:
serializeBooleanField(jsonGenerator, propertyAndEncoder, value);
break;
case OBJECT:
serializeJsonObjectField(jsonGenerator, propertyAndEncoder, value);
break;
default:
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
Expand Down Expand Up @@ -52,4 +55,19 @@ private void serializeBooleanField(JsonGenerator jsonGenerator, AbstractProperty
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
}


private void serializeJsonObjectField(JsonGenerator jsonGenerator, AbstractPropertyAndEncoder<T> propertyAndEncoder, String value) throws IOException {
String trimmed = value != null ? value.trim() : "";
if ("".equals(value)) {
jsonGenerator.writeFieldName(propertyAndEncoder.getName());
jsonGenerator.writeRawValue("{}");
} else if ((trimmed.startsWith("{") && trimmed.endsWith("}"))
|| (trimmed.startsWith("[") && trimmed.endsWith("]"))) {
jsonGenerator.writeFieldName(propertyAndEncoder.getName());
jsonGenerator.writeRawValue(trimmed);
} else {
serializeStringField(jsonGenerator, propertyAndEncoder, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Property {
private Type type = Type.STRING;

public enum Type {
STRING, INT, FLOAT, BOOLEAN
STRING, INT, FLOAT, BOOLEAN, OBJECT
}

public Property() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,49 @@ public void should_serialize_object_when_invalid_type() throws Exception {
// then
verify(jsonGenerator).writeObject("value");
}

@Test
public void should_serialize_object_as_raw_json() throws Exception {
Property property = new Property();
property.setName("args");
property.setValue("{\"name\": \"value\", \"serial\": 1} ");
property.setType("object");

org.mockito.Mockito.reset(jsonGenerator);
// when
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context));

// then
verify(jsonGenerator).writeRawValue("{\"name\": \"value\", \"serial\": 1}");
}

@Test
public void should_serialize_empty_object() throws Exception {
Property property = new Property();
property.setName("args");
property.setValue("");
property.setType("object");
property.setAllowEmpty(true);

// when
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context));

// then
verify(jsonGenerator).writeRawValue("{}");
}

@Test
public void should_serialize_invalid_object_as_text() throws Exception {
Property property = new Property();
property.setName("args2");
property.setValue("{\"name\": \"value\"");
property.setType("object");

org.mockito.Mockito.reset(jsonGenerator);
// when
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context));

// then
verify(jsonGenerator).writeObject("{\"name\": \"value\"");
}
}