Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
merchanjavier
committed
Oct 12, 2017
1 parent
3b4a726
commit 6024368
Showing
6 changed files
with
239 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/internetitem/logback/elasticsearch/PropertySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.internetitem.logback.elasticsearch; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.internetitem.logback.elasticsearch.util.AbstractPropertyAndEncoder; | ||
|
||
import java.io.IOException; | ||
|
||
class PropertySerializer<T> { | ||
private PropertySerializer() { | ||
} | ||
|
||
private static PropertySerializer INSTANCE = null; | ||
|
||
static synchronized PropertySerializer getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new PropertySerializer(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
synchronized void serializeProperty(JsonGenerator jsonGenerator, T event, AbstractPropertyAndEncoder<T> 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<T> propertyAndEncoder, String value) throws IOException { | ||
jsonGenerator.writeObjectField(propertyAndEncoder.getName(), value); | ||
} | ||
|
||
private void serializeIntField(JsonGenerator jsonGenerator, AbstractPropertyAndEncoder<T> 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<T> 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<T> propertyAndEncoder, String value) throws IOException { | ||
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) { | ||
jsonGenerator.writeBooleanField(propertyAndEncoder.getName(), Boolean.valueOf(value)); | ||
} else { | ||
serializeStringField(jsonGenerator, propertyAndEncoder, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/test/java/com/internetitem/logback/elasticsearch/PropertySerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.internetitem.logback.elasticsearch; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.Context; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.internetitem.logback.elasticsearch.config.Property; | ||
import com.internetitem.logback.elasticsearch.util.ClassicPropertyAndEncoder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class PropertySerializerTest { | ||
@Mock | ||
private Context context; | ||
|
||
@Mock | ||
private JsonGenerator jsonGenerator; | ||
|
||
@Mock | ||
private ILoggingEvent loggingEvent; | ||
|
||
private PropertySerializer<ILoggingEvent> propertySerializer = PropertySerializer.getInstance(); | ||
|
||
@Test | ||
public void should_default_to_string_type() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("propertyValue"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
assertThat(property.getType(), is(Property.Type.STRING)); | ||
verify(jsonGenerator).writeObject("propertyValue"); | ||
} | ||
|
||
@Test | ||
public void should_serialize_int_as_number() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("123"); | ||
property.setType("int"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeNumber(123); | ||
} | ||
|
||
@Test | ||
public void should_serialize_object_when_invalid_int() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("A123Z"); | ||
property.setType("int"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeObject("A123Z"); | ||
} | ||
|
||
@Test | ||
public void should_serialize_float_as_number() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("12.30"); | ||
property.setType("float"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeNumber(12.30f); | ||
} | ||
|
||
@Test | ||
public void should_serialize_object_when_invalid_float() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("A12.30Z"); | ||
property.setType("float"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeObject("A12.30Z"); | ||
} | ||
|
||
@Test | ||
public void should_serialize_true_as_boolean() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("true"); | ||
property.setType("boolean"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeBoolean(true); | ||
} | ||
|
||
@Test | ||
public void should_serialize_object_when_invalid_boolean() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("AtrueZ"); | ||
property.setType("boolean"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeObject("AtrueZ"); | ||
} | ||
|
||
@Test | ||
public void should_serialize_object_when_invalid_type() throws Exception { | ||
// given | ||
Property property = new Property(); | ||
property.setValue("value"); | ||
property.setType("invalidType"); | ||
|
||
// when | ||
propertySerializer.serializeProperty(jsonGenerator, loggingEvent, new ClassicPropertyAndEncoder(property, context)); | ||
|
||
// then | ||
verify(jsonGenerator).writeObject("value"); | ||
} | ||
} |