📄 ndcfilter.java
字号:
package com.sri.oaa2.simplefac;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.NDC;
/**
* NDCFilter is used in conjunction with a DOMConfigurator in a configuration file:
*
* <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
* <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
* <param name="Target" value="System.out" />
* <layout class="org.apache.log4j.PatternLayout">
* <param name="ConversionPattern" value="%r %5p [%x-%t] (%c{2}) - %m%n" />
* </layout>
* <filter class="com.sri.oaa2.simplefac.NDCFilter">
* <param name="AcceptOnMatch" value="false" />
* <param name="StringToMatch" value="NDC_NO_DISPLAY" />
* </filter>
* </appender>
* ...
*
* In this case, based on the NDC at the time a log is made, no logs for which the
* NDC contains the string NDC_NO_DISPLAY will be displayed.
*/
public class NDCFilter extends Filter
{
/**
* Accept when the string matches, or don't
*/
boolean acceptOnMatch = true;
/**
* The string to match
*/
String stringToMatch = null;
/**
* Set the value of acceptOnMatch
*/
public void setAcceptOnMatch(boolean a)
{
acceptOnMatch = a;
}
/**
* Does the filter accept the log when a match is found or not?
*/
public boolean getAcceptOnMatch()
{
return acceptOnMatch;
}
/**
* Set the string that is used to discover a match
*/
public void setStringToMatch(String s)
{
stringToMatch = s;
}
/**
* Get the string which is used as the filter
*/
public String getStringToMatch()
{
return stringToMatch;
}
/**
* Accept or deny the LoggingEvent based on the value of the NDC
*/
public int decide(LoggingEvent event)
{
if(stringToMatch == null) {
return Filter.NEUTRAL;
}
String ndcVal = NDC.get();
if(ndcVal == null) {
return Filter.NEUTRAL;
}
if(ndcVal.indexOf(getStringToMatch()) == -1) {
return Filter.NEUTRAL;
}
else {
if(getAcceptOnMatch()) {
return Filter.ACCEPT;
}
else {
return Filter.DENY;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -