Search in sources :

Example 1 with RewritePolicy

use of org.apache.log4j.rewrite.RewritePolicy in project logging-log4j2 by apache.

the class XmlConfiguration method parseRewritePolicy.

public RewritePolicy parseRewritePolicy(Element rewritePolicyElement) {
    String className = subst(rewritePolicyElement.getAttribute(CLASS_ATTR));
    LOGGER.debug("Class name: [" + className + ']');
    RewritePolicy policy = manager.parseRewritePolicy(className, rewritePolicyElement, this);
    if (policy == null) {
        policy = buildRewritePolicy(className, rewritePolicyElement);
    }
    return policy;
}
Also used : RewritePolicy(org.apache.log4j.rewrite.RewritePolicy)

Example 2 with RewritePolicy

use of org.apache.log4j.rewrite.RewritePolicy in project logging-log4j2 by apache.

the class XmlConfiguration method buildRewritePolicy.

private RewritePolicy buildRewritePolicy(String className, Element element) {
    try {
        RewritePolicy policy = LoaderUtil.newInstanceOf(className);
        PropertySetter propSetter = new PropertySetter(policy);
        forEachElement(element.getChildNodes(), (currentElement) -> {
            if (currentElement.getTagName().equalsIgnoreCase(PARAM_TAG)) {
                setParameter(currentElement, propSetter);
            }
        });
        propSetter.activate();
        return policy;
    } catch (ConsumerException ex) {
        Throwable t = ex.getCause();
        if (t instanceof InterruptedException || t instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        LOGGER.error("Could not create an RewritePolicy. Reported error follows.", t);
    } catch (Exception oops) {
        if (oops instanceof InterruptedException || oops instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        LOGGER.error("Could not create an RewritePolicy. Reported error follows.", oops);
    }
    return null;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PropertySetter(org.apache.log4j.config.PropertySetter) RewritePolicy(org.apache.log4j.rewrite.RewritePolicy) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 3 with RewritePolicy

use of org.apache.log4j.rewrite.RewritePolicy in project logging-log4j2 by apache.

the class RewriteAppenderBuilder method parseAppender.

@Override
public Appender parseAppender(final String name, final String appenderPrefix, final String layoutPrefix, final String filterPrefix, final Properties props, final PropertiesConfiguration configuration) {
    String appenderRef = getProperty(APPENDER_REF_TAG);
    Filter filter = configuration.parseAppenderFilters(props, filterPrefix, name);
    String policyPrefix = appenderPrefix + ".rewritePolicy";
    String className = getProperty(policyPrefix);
    RewritePolicy policy = configuration.getBuilderManager().parseRewritePolicy(className, policyPrefix, props, configuration);
    String level = getProperty(THRESHOLD_PARAM);
    if (appenderRef == null) {
        LOGGER.warn("No appender references configured for AsyncAppender {}", name);
        return null;
    }
    Appender appender = configuration.parseAppender(props, appenderRef);
    if (appender == null) {
        LOGGER.warn("Cannot locate Appender {}", appenderRef);
        return null;
    }
    return createAppender(name, level, new String[] { appenderRef }, policy, filter, configuration);
}
Also used : RewriteAppender(org.apache.logging.log4j.core.appender.rewrite.RewriteAppender) Appender(org.apache.log4j.Appender) Filter(org.apache.log4j.spi.Filter) RewritePolicy(org.apache.log4j.rewrite.RewritePolicy)

Example 4 with RewritePolicy

use of org.apache.log4j.rewrite.RewritePolicy in project logging-log4j2 by apache.

the class RewriteAppenderBuilder method parseAppender.

@Override
public Appender parseAppender(final Element appenderElement, final XmlConfiguration config) {
    String name = getNameAttribute(appenderElement);
    Holder<List<String>> appenderRefs = new Holder<>(new ArrayList<>());
    Holder<RewritePolicy> rewritePolicyHolder = new Holder<>();
    Holder<String> level = new Holder<>();
    Holder<Filter> filter = new Holder<>();
    forEachElement(appenderElement.getChildNodes(), (currentElement) -> {
        switch(currentElement.getTagName()) {
            case APPENDER_REF_TAG:
                Appender appender = config.findAppenderByReference(currentElement);
                if (appender != null) {
                    appenderRefs.get().add(appender.getName());
                }
                break;
            case REWRITE_POLICY_TAG:
                {
                    RewritePolicy policy = config.parseRewritePolicy(currentElement);
                    if (policy != null) {
                        rewritePolicyHolder.set(policy);
                    }
                    break;
                }
            case FILTER_TAG:
                {
                    filter.set(config.parseFilters(currentElement));
                    break;
                }
            case PARAM_TAG:
                {
                    if (getNameAttributeKey(currentElement).equalsIgnoreCase(THRESHOLD_PARAM)) {
                        String value = getValueAttribute(currentElement);
                        if (value == null) {
                            LOGGER.warn("No value supplied for Threshold parameter, ignoring.");
                        } else {
                            level.set(value);
                        }
                    }
                    break;
                }
        }
    });
    return createAppender(name, level.get(), appenderRefs.get().toArray(new String[0]), rewritePolicyHolder.get(), filter.get(), config);
}
Also used : RewriteAppender(org.apache.logging.log4j.core.appender.rewrite.RewriteAppender) Appender(org.apache.log4j.Appender) Filter(org.apache.log4j.spi.Filter) Holder(org.apache.log4j.builders.Holder) ArrayList(java.util.ArrayList) List(java.util.List) RewritePolicy(org.apache.log4j.rewrite.RewritePolicy)

Aggregations

RewritePolicy (org.apache.log4j.rewrite.RewritePolicy)4 Appender (org.apache.log4j.Appender)2 Filter (org.apache.log4j.spi.Filter)2 RewriteAppender (org.apache.logging.log4j.core.appender.rewrite.RewriteAppender)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Holder (org.apache.log4j.builders.Holder)1 PropertySetter (org.apache.log4j.config.PropertySetter)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1