Search in sources :

Example 1 with PropertySetter

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

the class XmlConfiguration method parseErrorHandler.

/**
 * Used internally to parse an {@link ErrorHandler} element.
 */
private void parseErrorHandler(Element element, Appender appender) {
    ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByClassName(subst(element.getAttribute(CLASS_ATTR)), ErrorHandler.class, null);
    if (eh != null) {
        eh.setAppender(appender);
        PropertySetter propSetter = new PropertySetter(eh);
        forEachElement(element.getChildNodes(), (currentElement) -> {
            String tagName = currentElement.getTagName();
            if (tagName.equals(PARAM_TAG)) {
                setParameter(currentElement, propSetter);
            }
        });
        propSetter.activate();
        appender.setErrorHandler(eh);
    }
}
Also used : ErrorHandler(org.apache.log4j.spi.ErrorHandler) PropertySetter(org.apache.log4j.config.PropertySetter)

Example 2 with PropertySetter

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

the class XmlConfiguration method parseChildrenOfLoggerElement.

/**
 * Used internally to parse the children of a LoggerConfig element.
 */
private void parseChildrenOfLoggerElement(Element catElement, LoggerConfig loggerConfig, boolean isRoot) {
    final PropertySetter propSetter = new PropertySetter(loggerConfig);
    loggerConfig.getAppenderRefs().clear();
    forEachElement(catElement.getChildNodes(), (currentElement) -> {
        switch(currentElement.getTagName()) {
            case APPENDER_REF_TAG:
                {
                    Appender appender = findAppenderByReference(currentElement);
                    String refName = subst(currentElement.getAttribute(REF_ATTR));
                    if (appender != null) {
                        LOGGER.debug("Adding appender named [{}] to loggerConfig [{}].", refName, loggerConfig.getName());
                        loggerConfig.addAppender(getAppender(refName), null, null);
                    } else {
                        LOGGER.debug("Appender named [{}] not found.", refName);
                    }
                    break;
                }
            case LEVEL_TAG:
            case PRIORITY_TAG:
                {
                    parseLevel(currentElement, loggerConfig, isRoot);
                    break;
                }
            case PARAM_TAG:
                {
                    setParameter(currentElement, propSetter);
                    break;
                }
            default:
                {
                    quietParseUnrecognizedElement(loggerConfig, currentElement, props);
                }
        }
    });
    propSetter.activate();
}
Also used : Appender(org.apache.log4j.Appender) PropertySetter(org.apache.log4j.config.PropertySetter)

Example 3 with PropertySetter

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

the class XmlConfiguration method parseElement.

/**
 * Creates an object and processes any nested param elements
 * but does not call activateOptions.  If the class also supports
 * UnrecognizedElementParser, the parseUnrecognizedElement method
 * will be call for any child elements other than param.
 *
 * @param element       element, may not be null.
 * @param props         properties
 * @param expectedClass interface or class expected to be implemented
 *                      by created class
 * @return created class or null.
 * @throws Exception thrown if the contain object should be abandoned.
 * @since 1.2.15
 */
public Object parseElement(final Element element, final Properties props, @SuppressWarnings("rawtypes") final Class expectedClass) throws Exception {
    String clazz = subst(element.getAttribute("class"), props);
    Object instance = OptionConverter.instantiateByClassName(clazz, expectedClass, null);
    if (instance != null) {
        PropertySetter propSetter = new PropertySetter(instance);
        NodeList children = element.getChildNodes();
        final int length = children.getLength();
        for (int loop = 0; loop < length; loop++) {
            Node currentNode = children.item(loop);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) currentNode;
                String tagName = currentElement.getTagName();
                if (tagName.equals("param")) {
                    setParameter(currentElement, propSetter, props);
                } else {
                    parseUnrecognizedElement(instance, currentElement, props);
                }
            }
        }
        return instance;
    }
    return null;
}
Also used : PropertySetter(org.apache.log4j.config.PropertySetter) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 4 with PropertySetter

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

the class XmlConfiguration method buildLayout.

private Layout buildLayout(String className, Element layout_element) {
    try {
        Layout layout = LoaderUtil.newInstanceOf(className);
        PropertySetter propSetter = new PropertySetter(layout);
        forEachElement(layout_element.getChildNodes(), (currentElement) -> {
            String tagName = currentElement.getTagName();
            if (tagName.equals(PARAM_TAG)) {
                setParameter(currentElement, propSetter);
            } else {
                try {
                    parseUnrecognizedElement(layout, currentElement, props);
                } catch (Exception ex) {
                    throw new ConsumerException(ex);
                }
            }
        });
        propSetter.activate();
        return layout;
    } catch (ConsumerException ce) {
        Throwable cause = ce.getCause();
        if (cause instanceof InterruptedException || cause instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        LOGGER.error("Could not create the Layout. Reported error follows.", cause);
    } catch (Exception oops) {
        if (oops instanceof InterruptedException || oops instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        LOGGER.error("Could not create the Layout. Reported error follows.", oops);
    }
    return null;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Layout(org.apache.log4j.Layout) PropertySetter(org.apache.log4j.config.PropertySetter) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 5 with PropertySetter

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

the class XmlConfiguration method buildAppender.

private Appender buildAppender(String className, Element appenderElement) {
    try {
        Appender appender = LoaderUtil.newInstanceOf(className);
        PropertySetter propSetter = new PropertySetter(appender);
        appender.setName(subst(appenderElement.getAttribute(NAME_ATTR)));
        forEachElement(appenderElement.getChildNodes(), (currentElement) -> {
            // Parse appender parameters
            switch(currentElement.getTagName()) {
                case PARAM_TAG:
                    setParameter(currentElement, propSetter);
                    break;
                case LAYOUT_TAG:
                    appender.setLayout(parseLayout(currentElement));
                    break;
                case FILTER_TAG:
                    Filter filter = parseFilters(currentElement);
                    if (filter != null) {
                        LOGGER.debug("Adding filter of type [{}] to appender named [{}]", filter.getClass(), appender.getName());
                        appender.addFilter(filter);
                    }
                    break;
                case ERROR_HANDLER_TAG:
                    parseErrorHandler(currentElement, appender);
                    break;
                case APPENDER_REF_TAG:
                    String refName = subst(currentElement.getAttribute(REF_ATTR));
                    if (appender instanceof AppenderAttachable) {
                        AppenderAttachable aa = (AppenderAttachable) appender;
                        Appender child = findAppenderByReference(currentElement);
                        LOGGER.debug("Attaching appender named [{}] to appender named [{}].", refName, appender.getName());
                        aa.addAppender(child);
                    } else {
                        LOGGER.error("Requesting attachment of appender named [{}] to appender named [{}]" + "which does not implement org.apache.log4j.spi.AppenderAttachable.", refName, appender.getName());
                    }
                    break;
                default:
                    try {
                        parseUnrecognizedElement(appender, currentElement, props);
                    } catch (Exception ex) {
                        throw new ConsumerException(ex);
                    }
            }
        });
        propSetter.activate();
        return appender;
    } catch (ConsumerException ex) {
        Throwable t = ex.getCause();
        if (t instanceof InterruptedException || t instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        LOGGER.error("Could not create an Appender. Reported error follows.", t);
    } catch (Exception oops) {
        if (oops instanceof InterruptedException || oops instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        LOGGER.error("Could not create an Appender. Reported error follows.", oops);
    }
    return null;
}
Also used : Appender(org.apache.log4j.Appender) InterruptedIOException(java.io.InterruptedIOException) Filter(org.apache.log4j.spi.Filter) PropertySetter(org.apache.log4j.config.PropertySetter) AppenderAttachable(org.apache.log4j.spi.AppenderAttachable) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

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