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

com.vlkan.log4j2.logstash.layout.resolver.ContextStackResolver Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.vlkan.log4j2.logstash.layout.resolver;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.LogEvent;

import java.util.regex.Pattern;

/**
 * Add Nested Diagnostic Context (NDC).
 */
public class ContextStackResolver implements TemplateResolver {

    private static final ContextStackResolver INSTANCE = new ContextStackResolver();

    private ContextStackResolver() {
        // Do nothing.
    }

    public static ContextStackResolver getInstance() {
        return INSTANCE;
    }

    @Override
    public String getName() {
        return "ndc";
    }

    @Override
    public JsonNode resolve(TemplateResolverContext context, LogEvent logEvent) {
        ThreadContext.ContextStack contextStack = logEvent.getContextStack();
        if (contextStack.getDepth() == 0) {
            return null;
        }
        Pattern itemPattern = context.getNdcPattern();
        ArrayNode contextStackNode = context.getObjectMapper().createArrayNode();
        for (String contextStackItem : contextStack.asList()) {
            boolean matches = itemPattern == null || itemPattern.matcher(contextStackItem).matches();
            if (matches) {
                contextStackNode.add(contextStackItem);
            }
        }
        return contextStackNode;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy