use of org.apache.log4j.spi.Filter in project pentaho-kettle by pentaho.
the class Log4jAppenderTest method addFilter.
@Test
public void addFilter() {
Appender appender = getAppender();
Filter filter = mock(Filter.class);
appender.addFilter(filter);
assertThat(appender.getFilter(), is(filter));
}
use of org.apache.log4j.spi.Filter in project JMRI by JMRI.
the class JLogoutputFrame method createAppender.
/**
* Creates the appender and adds it to all known Loggers whose additivity
* flag is false, incl. root logger
*
* @param aTextPane the pane that contains the appender
* @return A configured Appender
*/
public JTextPaneAppender createAppender(JTextPane aTextPane) {
JTextPaneAppender result = new JTextPaneAppender(myLayout, "Debug", myFilters.toArray(new Filter[0]), aTextPane);
// TODO: This a simple approach to add the new appender to all yet known Loggers.
// If Loggers are created dynamically later on or the the additivity flag of
// a logger changes, these Loggers probably wouldn't log to this appender. Solution is to
// override the DefaultLoggerFactory and the Logger's setAdditivity().
// Better solution is: Derivation of HierarchyEventListener (see mail on log4j user list "logging relative to webapp context path in tomcat" from Mi 19.03.2008 12:04)
Enumeration<?> en = LogManager.getCurrentLoggers();
while (en.hasMoreElements()) {
Object o = en.nextElement();
if (o instanceof Logger) {
Logger logger = (Logger) o;
if (!logger.getAdditivity()) {
logger.addAppender(result);
}
// if !logger.getAdditivity()
}
// if o instanceof Logger
}
// while ( en )
LogManager.getRootLogger().addAppender(result);
return result;
}
use of org.apache.log4j.spi.Filter in project datanucleus-core by datanucleus.
the class PluginParserTest method testRequireBundleVersionLogged.
public void testRequireBundleVersionLogged() {
final java.util.Set messages = new HashSet();
Logger.getLogger("DataNucleus.General").addAppender(new Appender() {
public void setName(String arg0) {
}
public void setLayout(Layout arg0) {
}
public void setErrorHandler(ErrorHandler arg0) {
}
public boolean requiresLayout() {
return false;
}
public String getName() {
return "testappender123";
}
public Layout getLayout() {
return null;
}
public Filter getFilter() {
return null;
}
public ErrorHandler getErrorHandler() {
return null;
}
public void doAppend(LoggingEvent arg0) {
if (arg0.getLevel() != Level.DEBUG) {
messages.add(arg0.getRenderedMessage());
}
}
public void close() {
}
public void clearFilters() {
}
public void addFilter(Filter arg0) {
}
});
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(clr, "EXCEPTION", true);
assertEquals(0, mgr.getExtensionPoints().length);
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST10.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST11.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST12.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST13.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST14.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST15.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST16.MF", null));
mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST17.MF", null));
mgr.resolveConstraints();
try {
assertEquals(3, messages.size());
assertTrue(messages.contains("Bundle \"org.datanucleus.plugin.test12\" requires \"org.datanucleus.plugin.test11\" version \"(1.2.0.b2\" but the resolved bundle has version \"1.2.0.b2\" which is outside the expected range."));
assertTrue(messages.contains("Bundle \"org.datanucleus.plugin.test13\" requires \"org.datanucleus.plugin.test11\" version \"(1.2.0.c1\" but the resolved bundle has version \"1.2.0.b2\" which is outside the expected range."));
assertTrue(messages.contains("Bundle \"org.datanucleus.plugin.test15\" requires \"org.datanucleus.plugin.test11\" version \"(1.0.0.b2,1.2.0.b2)\" but the resolved bundle has version \"1.2.0.b2\" which is outside the expected range."));
} finally {
Logger.getLogger("DataNucleus.General").removeAppender("testappender123");
}
}
use of org.apache.log4j.spi.Filter 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;
}
use of org.apache.log4j.spi.Filter in project logging-log4j2 by apache.
the class PropertiesConfiguration method addProperties.
public void addProperties(final Object obj, final String[] keys, final Properties props, final String prefix) {
final Properties edited = new Properties();
props.stringPropertyNames().stream().filter(name -> {
if (name.startsWith(prefix)) {
for (final String key : keys) {
if (name.equals(key)) {
return false;
}
}
return true;
}
return false;
}).forEach(name -> edited.put(name, props.getProperty(name)));
PropertySetter.setProperties(obj, edited, prefix + ".");
}
Aggregations