All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkiverse.loggingjson.JsonFormatter Maven / Gradle / Ivy

There is a newer version: 0.2.3
Show newest version
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