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;
}
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 + "]");
}
}
}
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);
}
}
}
}
}
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;
}
Aggregations