Search in sources :

Example 6 with PropertySetter

use of org.apache.log4j.config.PropertySetter 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 7 with PropertySetter

use of org.apache.log4j.config.PropertySetter in project logging-log4j2 by apache.

the class PropertyConfigurator method parseAppenderFilters.

void parseAppenderFilters(final Properties properties, final String appenderName, final Appender appender) {
    // extract filters and filter options from props into a hashtable mapping
    // the property name defining the filter class to a list of pre-parsed
    // name-value pairs associated to that filter
    final String filterPrefix = APPENDER_PREFIX + appenderName + ".filter.";
    final int fIdx = filterPrefix.length();
    final Hashtable filters = new Hashtable();
    final Enumeration e = properties.keys();
    String name = "";
    while (e.hasMoreElements()) {
        final String key = (String) e.nextElement();
        if (key.startsWith(filterPrefix)) {
            final int dotIdx = key.indexOf('.', fIdx);
            String filterKey = key;
            if (dotIdx != -1) {
                filterKey = key.substring(0, dotIdx);
                name = key.substring(dotIdx + 1);
            }
            Vector filterOpts = (Vector) filters.get(filterKey);
            if (filterOpts == null) {
                filterOpts = new Vector();
                filters.put(filterKey, filterOpts);
            }
            if (dotIdx != -1) {
                final String value = OptionConverter.findAndSubst(key, properties);
                filterOpts.add(new NameValue(name, value));
            }
        }
    }
    // sort filters by IDs, insantiate filters, set filter options,
    // add filters to the appender
    final Enumeration g = new SortedKeyEnumeration(filters);
    while (g.hasMoreElements()) {
        final String key = (String) g.nextElement();
        final String clazz = properties.getProperty(key);
        if (clazz != null) {
            LogLog.debug("Filter key: [" + key + "] class: [" + properties.getProperty(key) + "] props: " + filters.get(key));
            final Filter filter = (Filter) OptionConverter.instantiateByClassName(clazz, Filter.class, null);
            if (filter != null) {
                final PropertySetter propSetter = new PropertySetter(filter);
                final Vector v = (Vector) filters.get(key);
                final Enumeration filterProps = v.elements();
                while (filterProps.hasMoreElements()) {
                    final NameValue kv = (NameValue) filterProps.nextElement();
                    propSetter.setProperty(kv.key, kv.value);
                }
                propSetter.activate();
                LogLog.debug("Adding filter of type [" + filter.getClass() + "] to appender named [" + appender.getName() + "].");
                appender.addFilter(filter);
            }
        } else {
            LogLog.warn("Missing class definition for filter: [" + key + "]");
        }
    }
}
Also used : Enumeration(java.util.Enumeration) Filter(org.apache.log4j.spi.Filter) Hashtable(java.util.Hashtable) PropertySetter(org.apache.log4j.config.PropertySetter) Vector(java.util.Vector)

Example 8 with PropertySetter

use of org.apache.log4j.config.PropertySetter in project logging-log4j2 by apache.

the class PropertyConfigurator method parseCatsAndRenderers.

/**
 * Parse non-root elements, such non-root categories and renderers.
 */
protected void parseCatsAndRenderers(final Properties properties, final LoggerRepository loggerRepository) {
    final Enumeration enumeration = properties.propertyNames();
    while (enumeration.hasMoreElements()) {
        final String key = (String) enumeration.nextElement();
        if (key.startsWith(CATEGORY_PREFIX) || key.startsWith(LOGGER_PREFIX)) {
            String loggerName = null;
            if (key.startsWith(CATEGORY_PREFIX)) {
                loggerName = key.substring(CATEGORY_PREFIX.length());
            } else if (key.startsWith(LOGGER_PREFIX)) {
                loggerName = key.substring(LOGGER_PREFIX.length());
            }
            final String value = OptionConverter.findAndSubst(key, properties);
            final Logger logger = loggerRepository.getLogger(loggerName, loggerFactory);
            synchronized (logger) {
                parseCategory(properties, logger, key, loggerName, value);
                parseAdditivityForLogger(properties, logger, loggerName);
            }
        } else if (key.startsWith(RENDERER_PREFIX)) {
            final String renderedClass = key.substring(RENDERER_PREFIX.length());
            final String renderingClass = OptionConverter.findAndSubst(key, properties);
            if (loggerRepository instanceof RendererSupport) {
                RendererMap.addRenderer((RendererSupport) loggerRepository, renderedClass, renderingClass);
            }
        } else if (key.equals(THROWABLE_RENDERER_PREFIX)) {
            if (loggerRepository instanceof ThrowableRendererSupport) {
                final ThrowableRenderer tr = (ThrowableRenderer) OptionConverter.instantiateByKey(properties, THROWABLE_RENDERER_PREFIX, org.apache.log4j.spi.ThrowableRenderer.class, null);
                if (tr == null) {
                    LogLog.error("Could not instantiate throwableRenderer.");
                } else {
                    final PropertySetter setter = new PropertySetter(tr);
                    setter.setProperties(properties, THROWABLE_RENDERER_PREFIX + ".");
                    ((ThrowableRendererSupport) loggerRepository).setThrowableRenderer(tr);
                }
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ThrowableRendererSupport(org.apache.log4j.spi.ThrowableRendererSupport) RendererSupport(org.apache.log4j.spi.RendererSupport) ThrowableRendererSupport(org.apache.log4j.spi.ThrowableRendererSupport) PropertySetter(org.apache.log4j.config.PropertySetter) ThrowableRenderer(org.apache.log4j.spi.ThrowableRenderer)

Example 9 with PropertySetter

use of org.apache.log4j.config.PropertySetter in project logging-log4j2 by apache.

the class XmlConfiguration method parseFilters.

/**
 * Used internally to parse a filter element.
 * @param filterElement The Filter Element.
 * @return The Filter.
 */
public Filter parseFilters(Element filterElement) {
    String className = subst(filterElement.getAttribute(CLASS_ATTR));
    LOGGER.debug("Class name: [" + className + ']');
    Filter filter = manager.parseFilter(className, filterElement, this);
    if (filter == null) {
        PropertySetter propSetter = new PropertySetter(filter);
        forEachElement(filterElement.getChildNodes(), (currentElement) -> {
            String tagName = currentElement.getTagName();
            if (tagName.equals(PARAM_TAG)) {
                setParameter(currentElement, propSetter);
            } else {
                quietParseUnrecognizedElement(filter, currentElement, props);
            }
        });
        propSetter.activate();
    }
    return filter;
}
Also used : Filter(org.apache.log4j.spi.Filter) PropertySetter(org.apache.log4j.config.PropertySetter)

Aggregations

PropertySetter (org.apache.log4j.config.PropertySetter)9 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)3 Filter (org.apache.log4j.spi.Filter)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 Enumeration (java.util.Enumeration)2 Appender (org.apache.log4j.Appender)2 Hashtable (java.util.Hashtable)1 Vector (java.util.Vector)1 Layout (org.apache.log4j.Layout)1 RewritePolicy (org.apache.log4j.rewrite.RewritePolicy)1 AppenderAttachable (org.apache.log4j.spi.AppenderAttachable)1 ErrorHandler (org.apache.log4j.spi.ErrorHandler)1 RendererSupport (org.apache.log4j.spi.RendererSupport)1 ThrowableRenderer (org.apache.log4j.spi.ThrowableRenderer)1 ThrowableRendererSupport (org.apache.log4j.spi.ThrowableRendererSupport)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1