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

uk.gov.dwp.logging.logback.JsonEncoder Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package uk.gov.dwp.logging.logback;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.encoder.EncoderBase;
import uk.gov.dwp.logging.LogEntryBuilderFactory;

import java.util.Optional;

@SuppressWarnings("squid:S1168") // allow null to be returned from byte arrays
public class JsonEncoder extends EncoderBase {

    private String appName = "UNKNOWN";
    private String appVersion = "UNKNOWN";

    private final LogEntryBuilderFactory entryBuilderFactory;

    public JsonEncoder() {
        entryBuilderFactory = new LogEntryBuilderFactory(appName, appVersion);
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }

    @Override
    public byte[] headerBytes() {
        return null;
    }

    @Override
    public byte[] encode(ILoggingEvent event) {

        LogEntryBuilderFactory.LogEntryBuilder builder = entryBuilderFactory.getBuilder()
                .withAppName(appName)
                .withAppVersion(appVersion)
                .withLevel(event.getLevel().toString())
                .withMessage(event.getFormattedMessage())
                .withNameSpace(event.getLoggerName())
                .withTimeStamp(event.getTimeStamp())
                .withMeta("thread", event.getThreadName());

        Optional.ofNullable(getContext())
                .map(Context::getCopyOfPropertyMap)
                .ifPresent(map -> map.forEach(builder::withMeta));

        event.getMDCPropertyMap().forEach(builder::withMeta);

        Optional.ofNullable(event.getThrowableProxy())
                .map(ThrowableProxyUtil::asString)
                .ifPresent(p -> builder.withMeta("exception", p));

        return builder.build().getBytes();
    }

    @Override
    public byte[] footerBytes() {
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy