Search in sources :

Example 1 with AppenderAttachable

use of org.apache.log4j.spi.AppenderAttachable 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)

Example 2 with AppenderAttachable

use of org.apache.log4j.spi.AppenderAttachable in project fabric8 by jboss-fuse.

the class Log4jLogQuery method reconnectAppender.

@Override
public void reconnectAppender() {
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    AppenderAttachable appenderAttachable = null;
    if (loggerFactory instanceof AppenderAttachable) {
        appenderAttachable = (AppenderAttachable) loggerFactory;
    }
    if (appenderAttachable == null) {
        appenderAttachable = LogManager.getRootLogger();
    }
    if (appenderAttachable != null) {
        appender.setName("LogQuery");
        appenderAttachable.addAppender(appender);
    } else {
        LOG.error("No ILoggerFactory found so cannot attach appender!");
    }
}
Also used : ILoggerFactory(org.slf4j.ILoggerFactory) AppenderAttachable(org.apache.log4j.spi.AppenderAttachable)

Aggregations

AppenderAttachable (org.apache.log4j.spi.AppenderAttachable)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 Appender (org.apache.log4j.Appender)1 PropertySetter (org.apache.log4j.config.PropertySetter)1 Filter (org.apache.log4j.spi.Filter)1 ILoggerFactory (org.slf4j.ILoggerFactory)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1