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);
}
}
}
}
}
Aggregations