final String timeStampString = dateFormatter.format(new Date(record.getMillis()));
final String formattedMessage = formatMessage(record);
final StringWriter writer = new StringWriter();
final JsonGenerator json = jsonFactory.createGenerator(writer);
{
json.writeStartObject()
.write(JSON_KEY_VERSION, LOGSTASH_JSON_VERSION)
.write(JSON_KEY_TIMESTAMP, timeStampString)
.write(JSON_KEY_SOURCE_HOST, hostName)
.write(JSON_KEY_SOURCE, sourceName)
.write(JSON_KEY_LEVEL, record.getLevel().getName())
.write(JSON_KEY_MESSAGE, formattedMessage)
.write(JSON_KEY_THREAD_ID, record.getThreadID());
final String threadName = getThreadName(record.getThreadID());
if (null != threadName) {
json.write(JSON_KEY_THREAD_NAME, threadName);
}
if (null != record.getLoggerName()) {
json.write(JSON_KEY_LOGGER_NAME, record.getLoggerName());
}
if (null != record.getSourceClassName()) {
json.write(JSON_KEY_SOURCE_CLASS, record.getSourceClassName());
}
if (null != record.getSourceMethodName()) {
json.write(JSON_KEY_SOURCE_METHOD, record.getSourceMethodName());
}
if (!tags.isEmpty()) {
json.writeStartArray("tags");
for (String tag: tags) {
json.write(tag);
}
json.writeEnd();
}
final Throwable thrown = record.getThrown();
if (thrown != null) {
json.writeStartObject(JSON_KEY_EXCEPTION);
boolean withDescription = true;
writeThrowableToJson(json, thrown, withDescription);
json.writeEnd();
}
final Map<String, String> context = thrown != null ?
LoggingContext.getLastEnteredContext() :
LoggingContext.getContext();
if (context != null) {
writeContextToJson(json, context);
}
json.writeEnd();
}
json.flush();
writer.append('\n');
return writer.toString();
}