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

log configured values as integer #30

Merged
merged 4 commits into from
Jun 12, 2019
Merged
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
26 changes: 25 additions & 1 deletion src/main/java/de/siegmar/logbackgelf/GelfEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class GelfEncoder extends EncoderBase<ILoggingEvent> {
*/
private PatternLayout fullPatternLayout;

private boolean numbersAsString = true;

/**
* Additional, static fields to send to graylog. Defaults: none.
*/
Expand Down Expand Up @@ -172,6 +174,14 @@ public void setAppendNewline(final boolean appendNewline) {
this.appendNewline = appendNewline;
}

public boolean isNumbersAsString() {
return numbersAsString;
}

public void setNumbersAsString(final boolean numbersAsString) {
this.numbersAsString = numbersAsString;
}

public PatternLayout getShortPatternLayout() {
return shortPatternLayout;
}
Expand Down Expand Up @@ -216,8 +226,22 @@ private void addField(final Map<String, Object> dst, final String key, final Str
addWarn("staticField key '" + key + "' is illegal. "
+ "Keys must apply to regex ^[\\w.-]*$");
} else {
dst.put(key, value);
final Object processedValue = processValue(key, value);
if (processedValue != null) {
dst.put(key, processedValue);
}
}
}

private Object processValue(final String key, final String value) {
if (!numbersAsString) {
try {
return Double.valueOf(value);
} catch (final NumberFormatException e) {
return value;
}
}
return value;
}

@Override
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,50 @@ public void rootExceptionWithCause() throws IOException {
jsonNode.get("_root_cause_message").textValue());
}

@Test
public void numericValueAsNumber() throws IOException {
encoder.setNumbersAsString(false);
encoder.start();

final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = lc.getLogger(LOGGER_NAME);

final LoggingEvent event = simpleLoggingEvent(logger, null);

event.setMDCPropertyMap(ImmutableMap.of("http_status", "200"));

final String logMsg = encodeToStr(event);

final ObjectMapper om = new ObjectMapper();
final JsonNode jsonNode = om.readTree(logMsg);
basicValidation(jsonNode);
final JsonNode httpStatus = jsonNode.get("_http_status");
assertEquals(200, httpStatus.asDouble(), 0);
assertTrue(httpStatus.isNumber());
}

@Test
public void numericValueAsString() throws IOException {
encoder.setNumbersAsString(true);
encoder.start();

final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = lc.getLogger(LOGGER_NAME);

final LoggingEvent event = simpleLoggingEvent(logger, null);

event.setMDCPropertyMap(ImmutableMap.of("http_status", "200"));

final String logMsg = encodeToStr(event);

final ObjectMapper om = new ObjectMapper();
final JsonNode jsonNode = om.readTree(logMsg);
basicValidation(jsonNode);
final JsonNode httpStatus = jsonNode.get("_http_status");
assertEquals("200", httpStatus.asText());
assertFalse(httpStatus.isNumber());
}

private String encodeToStr(final LoggingEvent event) {
return new String(encoder.encode(event), StandardCharsets.UTF_8);
}
Expand Down