Skip to content

Commit

Permalink
spot bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Nov 29, 2021
1 parent 8bd7b20 commit ea99467
Showing 1 changed file with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ private void performLogging(LogLevel logLevel, String format, Object... args) {
* Serializes passed map to string containing valid JSON fragment:
* e.g. "k1":"v1","k2":"v2", properly escaped and without trailing comma.
*
* Does not support custom or complex object serialization, uses {@code toString()} on them.
* For complex object serialization, it calls {@code toString()} guarded with null check.
*
* @param context to serialize.
*
* @returns Serialized JSON fragment or empty string.
* @returns Serialized JSON fragment or an empty string.
*/
static String writeJsonFragment(Map<String, Object> context) {
if (CoreUtils.isNullOrEmpty(context)) {
Expand All @@ -399,20 +399,36 @@ private static StringBuilder writeKeyAndValue(String key, Object value, StringBu
return formatter.append("null");
}

// LoggingEventBuilder only populates primitives and Strings
if (value instanceof Boolean
|| value instanceof Integer
|| value instanceof Long
|| value instanceof Byte) {
JSON_STRING_ENCODER.quoteAsString(value.toString(), formatter);
return formatter;
if (isPrimitive(value)) {
JSON_STRING_ENCODER.quoteAsString(value.toString(), formatter);
return formatter;
}

formatter.append("\"");
JSON_STRING_ENCODER.quoteAsString(value.toString(), formatter);
return formatter.append("\"");
}

/**
* Returns true if the value is an instance of a primitive type and false otherwise.
*/
private static boolean isPrimitive(Object value) {
// most of the time values are strings
if (value instanceof String) {
return false;
}

if (value instanceof Boolean
|| value instanceof Integer
|| value instanceof Long
|| value instanceof Byte
|| value instanceof Double
|| value instanceof Float) {
return true;
}

return false;
}

private static final class ContextKeyValuePair {
private final String key;
Expand Down

0 comments on commit ea99467

Please sign in to comment.