Search in sources :

Example 1 with Filter

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));
}
Also used : Appender(org.apache.log4j.Appender) Filter(org.apache.log4j.spi.Filter) Test(org.junit.Test)

Example 2 with 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;
}
Also used : Filter(org.apache.log4j.spi.Filter) Logger(org.apache.log4j.Logger)

Example 3 with Filter

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");
    }
}
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 4 with Filter

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;
}
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 5 with Filter

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 + ".");
}
Also used : LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) AppenderWrapper(org.apache.log4j.bridge.AppenderWrapper) Enumeration(java.util.Enumeration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) BuilderManager(org.apache.log4j.builders.BuilderManager) LoaderUtil(org.apache.logging.log4j.util.LoaderUtil) HashMap(java.util.HashMap) AbstractFilterable(org.apache.logging.log4j.core.filter.AbstractFilterable) ArrayList(java.util.ArrayList) Configuration(org.apache.logging.log4j.core.config.Configuration) Appender(org.apache.log4j.Appender) StringTokenizer(java.util.StringTokenizer) Map(java.util.Map) Properties(java.util.Properties) ErrorHandler(org.apache.log4j.spi.ErrorHandler) StatusConfiguration(org.apache.logging.log4j.core.config.status.StatusConfiguration) IOException(java.io.IOException) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) Filter(org.apache.log4j.spi.Filter) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) List(java.util.List) TreeMap(java.util.TreeMap) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) LogManager(org.apache.log4j.LogManager) OptionConverter(org.apache.log4j.helpers.OptionConverter) SortedMap(java.util.SortedMap) InputStream(java.io.InputStream) ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) Properties(java.util.Properties)

Aggregations

Filter (org.apache.log4j.spi.Filter)31 Appender (org.apache.log4j.Appender)14 Layout (org.apache.log4j.Layout)9 LoggingEvent (org.apache.log4j.spi.LoggingEvent)7 ArrayList (java.util.ArrayList)5 WriterAppender (org.apache.log4j.WriterAppender)5 Enumeration (java.util.Enumeration)4 List (java.util.List)4 Logger (org.apache.log4j.Logger)4 FileAppender (org.apache.log4j.FileAppender)3 PatternLayout (org.apache.log4j.PatternLayout)3 FilterWrapper (org.apache.log4j.bridge.FilterWrapper)3 PropertySetter (org.apache.log4j.config.PropertySetter)3 ErrorHandler (org.apache.log4j.spi.ErrorHandler)3 LevelMatchFilter (org.apache.log4j.varia.LevelMatchFilter)3 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)3 NullAppender (org.apache.log4j.varia.NullAppender)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2