Skip to content

Commit

Permalink
Merge pull request #39 from tomtom-international/feature/console-log
Browse files Browse the repository at this point in the history
feat: align console/file format to be more inline with kinesis format
  • Loading branch information
piotrwielgolaski-tomtom authored Jan 18, 2021
2 parents 2fafe7f + b8b69c3 commit c889649
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getId() {
@Override
public void initialize(EventPublisherConfiguration eventPublisherConfiguration) {
ConsolePublisherConfiguration configuration = new ConsolePublisherConfiguration(eventPublisherConfiguration);
formatter = new JSONEventFormatter(configuration.isPrettifyJSON());
formatter = new JSONEventFormatter(configuration.isPrettifyJSON(), configuration.getEventType(), configuration.getEnvironment());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.tomtom.james.common.api.configuration.EventPublisherConfiguration;
import com.tomtom.james.common.api.configuration.StructuredConfiguration;
import java.util.Optional;

class ConsolePublisherConfiguration {

Expand All @@ -34,4 +35,14 @@ boolean isPrettifyJSON() {
.orElse(false);
}

public String getEventType() {
return configurationProperties.get("eventType")
.map(StructuredConfiguration::asString)
.orElse("james");
}

public Optional<String> getEnvironment() {
return configurationProperties.get("environment")
.map(StructuredConfiguration::asString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getId() {
public void initialize(EventPublisherConfiguration eventPublisherConfiguration) {
FilePublisherConfiguration configuration = new FilePublisherConfiguration(eventPublisherConfiguration);
writer = createWriter(configuration);
formatter = new JSONEventFormatter(configuration.isPrettifyJSON());
formatter = new JSONEventFormatter(configuration.isPrettifyJSON(), configuration.getEventType(), configuration.getEnvironment());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Optional;

class FilePublisherConfiguration {

Expand Down Expand Up @@ -49,4 +50,14 @@ private String defaultFilePath() {
return path.normalize().toString();
}

public String getEventType() {
return configurationProperties.get("eventType")
.map(StructuredConfiguration::asString)
.orElse("james");
}

public Optional<String> getEnvironment() {
return configurationProperties.get("environment")
.map(StructuredConfiguration::asString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,29 @@

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Optional;

class JSONEventFormatter {

private final ObjectMapper objectMapper = new ObjectMapper();

private final boolean pretty;
private String type;
private Optional<String> environment;

JSONEventFormatter(boolean pretty) {
JSONEventFormatter(boolean pretty, String type, Optional<String> environment) {
this.pretty = pretty;
this.type = type;
this.environment = environment;
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}

String format(Event evt) {
LinkedHashMap<String, Object> result = new LinkedHashMap<>();
result.put("timestamp", evt.getTimestamp().toString());
result.put("content", evt.getContent());
result.put("@timestamp", evt.getTimestamp().toString());
result.put("type", type);
environment.ifPresent(env -> result.put("environment", env));
result.putAll(evt.getContent());

try {
return pretty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConsolePublisherSpec extends Specification {
publisher.close()

then:
1 * out.println { it.contains('"content":{"message":"event body"}') }
1 * out.println { it.contains('"type":"james","message":"event body"') }

then:
1 * out.flush()
Expand Down

0 comments on commit c889649

Please sign in to comment.