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);
}
}
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();
}
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;
}
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;
}
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;
}
Aggregations