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