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

no.entur.logging.cloud.appender.scope.LoggingScopeAsyncAppender Maven / Gradle / Ivy

The newest version!
package no.entur.logging.cloud.appender.scope;

import ch.qos.logback.classic.spi.ILoggingEvent;
import no.entur.logging.cloud.appender.MdcAsyncAppender;
import org.slf4j.Marker;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public class LoggingScopeAsyncAppender extends MdcAsyncAppender implements LoggingScopeSink {

    private List scopeProviders = new ArrayList<>();

    public void addScopeProvider(LoggingScopeProvider scopeProvider) {
        this.scopeProviders.add(scopeProvider);
    }

    @Override
    protected void append(ILoggingEvent eventObject) {
        if (isQueueBelowDiscardingThreshold() && isDiscardable(eventObject)) {
            return;
        }
        preprocess(eventObject);

        LoggingScope scope = getCurrentScope();
        if(scope == null || !scope.append(eventObject)) {
            postProcess(eventObject);
            put(eventObject);
        }
    }

    public LoggingScope getCurrentScope() {
        for (LoggingScopeProvider loggingScopeFactory : scopeProviders) {
            LoggingScope scope = loggingScopeFactory.getCurrentScope();
            if(scope != null) {
                return scope;
            }
        }
        return null;
    }

    public void write(LoggingScope scope) {
        ConcurrentLinkedQueue events = scope.getEvents();
        for (ILoggingEvent eventObject : events) {
            postProcess(eventObject);
            put(eventObject);
        }
    }

    private void postProcess(ILoggingEvent eventObject) {
        List markerList = eventObject.getMarkerList();
        if(markerList != null && !markerList.isEmpty()) {
            for (Marker marker : markerList) {
                if(marker instanceof LoggingScopePostProcessing postProcessing) {
                    postProcessing.performPostProcessing();
                }
            }
        }
    }

    public List getScopeProviders() {
        return scopeProviders;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy