io.quarkiverse.loggingjson.JsonFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkiverse-logging-json Show documentation
Show all versions of quarkiverse-logging-json Show documentation
Logging in json with support for custom fields
package io.quarkiverse.loggingjson;
import java.util.List;
import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;
public class JsonFormatter extends ExtFormatter {
private final StringBuilderWriter writer = new StringBuilderWriter();
private final List providers;
private final JsonFactory jsonFactory;
private String recordDelimiter;
public JsonFormatter(List providers, JsonFactory jsonFactory, Config config) {
this.providers = providers;
this.jsonFactory = jsonFactory;
this.recordDelimiter = config.recordDelimiter;
}
@Override
public String format(ExtLogRecord record) {
try {
try (JsonGenerator generator = this.jsonFactory.createGenerator(writer)) {
generator.writeStartObject();
for (JsonProvider provider : this.providers) {
provider.writeTo(generator, record);
}
generator.writeEndObject();
generator.flush();
if (recordDelimiter != null) {
writer.write(recordDelimiter);
}
}
return writer.toString();
} catch (Exception e) {
// Wrap and rethrow
throw new RuntimeException(e);
} finally {
// Clear the writer for the next format
writer.clear();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy