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

io.quarkiverse.loggingjson.providers.AdditionalFieldsJsonProvider Maven / Gradle / Ivy

There is a newer version: 0.2.3
Show newest version
package io.quarkiverse.loggingjson.providers;

import java.io.IOException;
import java.util.Map;

import org.jboss.logmanager.ExtLogRecord;

import io.quarkiverse.loggingjson.Config;
import io.quarkiverse.loggingjson.Enabled;
import io.quarkiverse.loggingjson.JsonGenerator;
import io.quarkiverse.loggingjson.JsonProvider;

public class AdditionalFieldsJsonProvider implements JsonProvider, Enabled {
    private final boolean enabled;
    private final Map fields;

    public AdditionalFieldsJsonProvider(Map fields) {
        this.enabled = fields != null && !fields.isEmpty();
        this.fields = fields;
    }

    @Override
    public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {
        for (Map.Entry entry : fields.entrySet()) {
            final String fieldName = entry.getKey();
            final String fieldValue = entry.getValue().value;
            switch (entry.getValue().type) {
                case STRING:
                    generator.writeStringField(fieldName, fieldValue);
                    break;
                case INT:
                    generator.writeNumberField(fieldName, Integer.parseInt(fieldValue));
                    break;
                case LONG:
                    generator.writeNumberField(fieldName, Long.parseLong(fieldValue));
                    break;
                case FLOAT:
                    generator.writeNumberField(fieldName, Float.parseFloat(fieldValue));
                    break;
                case DOUBLE:
                    generator.writeNumberField(fieldName, Double.parseDouble(fieldValue));
                    break;
            }
        }
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy