Search in sources :

Example 1 with LogfileAppender

use of org.knime.core.util.LogfileAppender 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 : LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) WriterAppender(org.apache.log4j.WriterAppender) FileAppender(org.apache.log4j.FileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Filter(org.apache.log4j.spi.Filter) LogfileAppender(org.knime.core.util.LogfileAppender) Logger(org.apache.log4j.Logger) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

File (java.io.File)1 Appender (org.apache.log4j.Appender)1 FileAppender (org.apache.log4j.FileAppender)1 Logger (org.apache.log4j.Logger)1 WriterAppender (org.apache.log4j.WriterAppender)1 Filter (org.apache.log4j.spi.Filter)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)1 NullAppender (org.apache.log4j.varia.NullAppender)1 ReferencedFile (org.knime.core.internal.ReferencedFile)1 LogfileAppender (org.knime.core.util.LogfileAppender)1