Search in sources :

Example 16 with Filter

use of org.apache.log4j.spi.Filter in project gocd by gocd.

the class LoggingHelper method createConsoleFileAppender.

public static Appender createConsoleFileAppender(final CONSOLE_NDC ndc, String consoleFileName) {
    Appender consoleAppender = createFileAppender(consoleFileName);
    consoleAppender.setName(ndc.name());
    consoleAppender.setLayout(LOG4J_CONSOLE_PATTERN);
    //Console NDC acceptance filter
    consoleAppender.addFilter(new org.apache.log4j.spi.Filter() {

        @Override
        public int decide(LoggingEvent event) {
            return event.getNDC() == ndc.toString() ? ACCEPT : DENY;
        }
    });
    addConsoleNDCLogRemoverFilter();
    return consoleAppender;
}
Also used : Appender(org.apache.log4j.Appender) RollingFileAppender(org.apache.log4j.RollingFileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) Filter(org.apache.log4j.spi.Filter)

Example 17 with Filter

use of org.apache.log4j.spi.Filter in project gocd by gocd.

the class LoggingHelper method addConsoleNDCLogRemoverFilter.

private static void addConsoleNDCLogRemoverFilter() {
    Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();
    while (allAppenders.hasMoreElements()) {
        Appender appender = (Appender) allAppenders.nextElement();
        if (isConsoleNdcAppender(appender)) {
            continue;
        }
        Filter filter = getConsoleNDCFilter(appender);
        if (filter != CONSOLE_NDC_REJECT_FILTER) {
            appender.addFilter(CONSOLE_NDC_REJECT_FILTER);
        }
    }
}
Also used : Appender(org.apache.log4j.Appender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Enumeration(java.util.Enumeration) Filter(org.apache.log4j.spi.Filter)

Example 18 with Filter

use of org.apache.log4j.spi.Filter in project knime-core by knime.

the class NodeLogger method addWorkflowDirAppender.

/**
 * Adds a new workflow directory logger for the given workflow directory if it doesn't exists yet.
 * @param workflowDir the directory of the workflow that should be logged to
 */
private void addWorkflowDirAppender(final File workflowDir) {
    if (workflowDir == null) {
        // if the workflowDir is null we do not need to append an extra log appender
        return;
    }
    // in this method we have to use the logger directly to prevent a deadlock!!!
    final Logger logger = m_logger;
    final String workflowDirPath = workflowDir.getPath();
    if (workflowDirPath == null) {
        return;
    }
    Appender wfAppender = WF_APPENDER.get(workflowDirPath);
    if (wfAppender != null) {
        logger.addAppender(wfAppender);
    } else {
        // we do the getAppender twice to prevent the synchronize block on subsequent calls!!!
        synchronized (WF_APPENDER) {
            // we need a synchronize block otherwise we might create a second appender that opens a file handle
            // which never get closed and thus the copying of a full log file to the zip file fails
            wfAppender = WF_APPENDER.get(workflowDirPath);
            if (wfAppender == null) {
                // use the KNIME specific LogfielAppender that moves larger log files into a separate zip file
                // and that implements equals and hash code to ensure that two LogfileAppender
                // with the same name are considered equal to prevent duplicate appender registration
                final FileAppender fileAppender = new LogfileAppender(workflowDir);
                fileAppender.setLayout(WF_DIR_LOG_FILE_LAYOUT);
                fileAppender.setName(workflowDirPath);
                final Filter mainFilter = LOG_FILE_APPENDER.getFilter();
                fileAppender.addFilter(new Filter() {

                    @Override
                    public int decide(final LoggingEvent event) {
                        final Object msg = event.getMessage();
                        if (msg instanceof KNIMELogMessage) {
                            final KNIMELogMessage kmsg = (KNIMELogMessage) msg;
                            // can be null
                            final File msgDir = kmsg.getWorkflowDir();
                            if ((LOG_GLOBAL_IN_WF_DIR && msgDir == null) || LOG_IN_WF_DIR && workflowDir.equals(msgDir)) {
                                // return only neutral to let the log level based filters decide if we log this event
                                if (mainFilter != null) {
                                    return mainFilter.decide(event);
                                }
                                return Filter.NEUTRAL;
                            }
                        }
                        return Filter.DENY;
                    }
                });
                // we have to call this function to activate the writer!!!
                fileAppender.activateOptions();
                logger.addAppender(fileAppender);
                WF_APPENDER.put(workflowDirPath, fileAppender);
                if (m_listener == null) {
                    m_listener = new MyWorkflowListener();
                    WorkflowManager.ROOT.addListener(m_listener);
                }
            }
        }
    }
}
Also used : WriterAppender(org.apache.log4j.WriterAppender) LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Filter(org.apache.log4j.spi.Filter) LevelMatchFilter(org.apache.log4j.varia.LevelMatchFilter) LogfileAppender(org.knime.core.util.LogfileAppender) Logger(org.apache.log4j.Logger) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 19 with Filter

use of org.apache.log4j.spi.Filter in project knime-core by knime.

the class NodeLogger method setAppenderLevelRange.

/**
 * Sets a level range filter on the given appender.
 *
 * @param appenderName the name of the appender
 * @param min the minimum logging level
 * @param max the maximum logging level
 * @throws NoSuchElementException if the given appender does not exist
 * @since 2.8
 */
public static void setAppenderLevelRange(final String appenderName, final LEVEL min, final LEVEL max) throws NoSuchElementException {
    Logger root = Logger.getRootLogger();
    Appender appender = root.getAppender(appenderName);
    if (appender == null) {
        throw new NoSuchElementException("Appender '" + appenderName + "' does not exist");
    }
    Filter filter = appender.getFilter();
    while ((filter != null) && !(filter instanceof LevelRangeFilter)) {
        filter = filter.getNext();
    }
    if (filter == null) {
        // add a new level range filter
        LevelRangeFilter levelFilter = new LevelRangeFilter();
        levelFilter.setLevelMin(transLEVEL(min));
        levelFilter.setLevelMax(transLEVEL(max));
        appender.addFilter(levelFilter);
    } else {
        // modify existing level range filter
        ((LevelRangeFilter) filter).setLevelMin(transLEVEL(min));
        ((LevelRangeFilter) filter).setLevelMax(transLEVEL(max));
    }
    updateLog4JKNIMELoggerLevel();
}
Also used : WriterAppender(org.apache.log4j.WriterAppender) LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Filter(org.apache.log4j.spi.Filter) LevelMatchFilter(org.apache.log4j.varia.LevelMatchFilter) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Logger(org.apache.log4j.Logger) NoSuchElementException(java.util.NoSuchElementException)

Example 20 with Filter

use of org.apache.log4j.spi.Filter in project xtext-eclipse by eclipse.

the class LoggingTester method removeFilter.

private static void removeFilter(Appender appender, Filter filter) {
    if (Objects.equal(appender.getFilter(), filter)) {
        appender.clearFilters();
        appender.addFilter(filter.getNext());
    } else {
        for (Filter current = appender.getFilter(); (current != null); current = current.getNext()) {
            if (Objects.equal(current.getNext(), filter)) {
                current.setNext(filter.getNext());
                return;
            }
        }
    }
}
Also used : Filter(org.apache.log4j.spi.Filter)

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