Search in sources :

Example 1 with ErrorHandler

use of org.apache.log4j.spi.ErrorHandler 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");
    }
}
Also used : Appender(org.apache.log4j.Appender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ErrorHandler(org.apache.log4j.spi.ErrorHandler) Layout(org.apache.log4j.Layout) Filter(org.apache.log4j.spi.Filter) NonManagedPluginRegistry(org.datanucleus.plugin.NonManagedPluginRegistry) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) HashSet(java.util.HashSet) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 2 with ErrorHandler

use of org.apache.log4j.spi.ErrorHandler 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 3 with ErrorHandler

use of org.apache.log4j.spi.ErrorHandler in project logging-log4j2 by apache.

the class PropertiesConfiguration method buildAppender.

private Appender buildAppender(final String appenderName, final String className, final String prefix, final String layoutPrefix, final String filterPrefix, final Properties props) {
    final Appender appender = newInstanceOf(className, "Appender");
    if (appender == null) {
        return null;
    }
    appender.setName(appenderName);
    appender.setLayout(parseLayout(layoutPrefix, appenderName, props));
    final String errorHandlerPrefix = prefix + ".errorhandler";
    final String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, props);
    if (errorHandlerClass != null) {
        final ErrorHandler eh = parseErrorHandler(props, errorHandlerPrefix, errorHandlerClass, appender);
        if (eh != null) {
            appender.setErrorHandler(eh);
        }
    }
    appender.addFilter(parseAppenderFilters(props, filterPrefix, appenderName));
    final String[] keys = new String[] { layoutPrefix };
    addProperties(appender, keys, props, prefix);
    if (appender instanceof AppenderWrapper) {
        addAppender(((AppenderWrapper) appender).getAppender());
    } else {
        addAppender(new AppenderAdapter(appender).getAdapter());
    }
    registry.put(appenderName, appender);
    return appender;
}
Also used : Appender(org.apache.log4j.Appender) ErrorHandler(org.apache.log4j.spi.ErrorHandler) AppenderWrapper(org.apache.log4j.bridge.AppenderWrapper) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter)

Example 4 with ErrorHandler

use of org.apache.log4j.spi.ErrorHandler in project logging-log4j2 by apache.

the class PropertiesConfiguration method parseErrorHandler.

public ErrorHandler parseErrorHandler(final Properties props, final String errorHandlerPrefix, final String errorHandlerClass, final Appender appender) {
    final ErrorHandler eh = newInstanceOf(errorHandlerClass, "ErrorHandler");
    final String[] keys = new String[] { // @formatter:off
    errorHandlerPrefix + "." + ROOT_REF, errorHandlerPrefix + "." + LOGGER_REF, errorHandlerPrefix + "." + APPENDER_REF_TAG };
    // @formatter:on
    addProperties(eh, keys, props, errorHandlerPrefix);
    return eh;
}
Also used : ErrorHandler(org.apache.log4j.spi.ErrorHandler)

Example 5 with ErrorHandler

use of org.apache.log4j.spi.ErrorHandler in project datanucleus-core by datanucleus.

the class PluginParserTest method testRequireBundleLogged.

public void testRequireBundleLogged() {
    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.getRenderedMessage().indexOf("but it cannot be resolved") > 0) {
                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/MANIFEST3.MF", null));
    mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST4.MF", null));
    mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST5.MF", null));
    mgr.resolveConstraints();
    try {
        assertEquals(2, messages.size());
        assertTrue(messages.contains("Bundle \"org.datanucleus.plugin.test5\" requires \"org.datanucleus.plugin.test6\" but it cannot be resolved."));
        assertTrue(messages.contains("Bundle \"org.datanucleus.plugin.test5\" has an optional dependency to \"org.datanucleus.plugin.test7\" but it cannot be resolved"));
    } finally {
        Logger.getLogger("DataNucleus.General").removeAppender("testappender123");
    }
}
Also used : Appender(org.apache.log4j.Appender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ErrorHandler(org.apache.log4j.spi.ErrorHandler) Layout(org.apache.log4j.Layout) Filter(org.apache.log4j.spi.Filter) NonManagedPluginRegistry(org.datanucleus.plugin.NonManagedPluginRegistry) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) HashSet(java.util.HashSet) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

ErrorHandler (org.apache.log4j.spi.ErrorHandler)6 Appender (org.apache.log4j.Appender)3 HashSet (java.util.HashSet)2 Layout (org.apache.log4j.Layout)2 Filter (org.apache.log4j.spi.Filter)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)2 NonManagedPluginRegistry (org.datanucleus.plugin.NonManagedPluginRegistry)2 Map (java.util.Map)1 Properties (java.util.Properties)1 AppenderAdapter (org.apache.log4j.bridge.AppenderAdapter)1 AppenderWrapper (org.apache.log4j.bridge.AppenderWrapper)1 PropertySetter (org.apache.log4j.config.PropertySetter)1 RendererMap (org.apache.log4j.or.RendererMap)1 OptionHandler (org.apache.log4j.spi.OptionHandler)1