logback的DuplicateMessageFilter日志過濾操作源碼解讀
序
本文主要研究一下logback的DuplicateMessageFilter
TurboFilter
ch/qos/logback/classic/turbo/TurboFilter.java
public abstract class TurboFilter extends ContextAwareBase implements LifeCycle {
private String name;
boolean start = false;
/**
* Make a decision based on the multiple parameters passed as arguments. The
* returned value should be one of <code>{@link FilterReply#DENY}</code>,
* <code>{@link FilterReply#NEUTRAL}</code>, or
* <code>{@link FilterReply#ACCEPT}</code>.
*
* @param marker
* @param logger
* @param level
* @param format
* @param params
* @param t
* @return
*/
public abstract FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params,
Throwable t);
public void start() {
this.start = true;
}
public boolean isStarted() {
return this.start;
}
public void stop() {
this.start = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}TurboFilter繼承了ContextAwareBase,聲明實現(xiàn)LifeCycle接口,它定義了decide方法由子類實現(xiàn)
DuplicateMessageFilter
ch/qos/logback/classic/turbo/DuplicateMessageFilter.java
public class DuplicateMessageFilter extends TurboFilter {
/**
* The default cache size.
*/
public static final int DEFAULT_CACHE_SIZE = 100;
/**
* The default number of allows repetitions.
*/
public static final int DEFAULT_ALLOWED_REPETITIONS = 5;
public int allowedRepetitions = DEFAULT_ALLOWED_REPETITIONS;
public int cacheSize = DEFAULT_CACHE_SIZE;
private LRUMessageCache msgCache;
@Override
public void start() {
msgCache = new LRUMessageCache(cacheSize);
super.start();
}
@Override
public void stop() {
msgCache.clear();
msgCache = null;
super.stop();
}
@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
int count = msgCache.getMessageCountAndThenIncrement(format);
if (count <= allowedRepetitions) {
return FilterReply.NEUTRAL;
} else {
return FilterReply.DENY;
}
}
public int getAllowedRepetitions() {
return allowedRepetitions;
}
/**
* The allowed number of repetitions before
*
* @param allowedRepetitions
*/
public void setAllowedRepetitions(int allowedRepetitions) {
this.allowedRepetitions = allowedRepetitions;
}
public int getCacheSize() {
return cacheSize;
}
public void setCacheSize(int cacheSize) {
this.cacheSize = cacheSize;
}
}DuplicateMessageFilter繼承了TurboFilter,它使用了LRUMessageCache(默認(rèn)大小100)來緩存format,decide方法會執(zhí)行g(shù)etMessageCountAndThenIncrement,若超出allowedRepetitions(默認(rèn)5)則返回FilterReply.DENY,否則返回FilterReply.NEUTRAL
LRUMessageCache
ch/qos/logback/classic/turbo/LRUMessageCache.java
class LRUMessageCache extends LinkedHashMap<String, Integer> {
private static final long serialVersionUID = 1L;
final int cacheSize;
LRUMessageCache(int cacheSize) {
super((int) (cacheSize * (4.0f / 3)), 0.75f, true);
if (cacheSize < 1) {
throw new IllegalArgumentException("Cache size cannot be smaller than 1");
}
this.cacheSize = cacheSize;
}
int getMessageCountAndThenIncrement(String msg) {
// don't insert null elements
if (msg == null) {
return 0;
}
Integer i;
// LinkedHashMap is not LinkedHashMap. See also LBCLASSIC-255
synchronized (this) {
i = super.get(msg);
if (i == null) {
i = 0;
} else {
i = i + 1;
}
super.put(msg, i);
}
return i;
}
// called indirectly by get() or put() which are already supposed to be
// called from within a synchronized block
protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
return (size() > cacheSize);
}
@Override
synchronized public void clear() {
super.clear();
}
}LRUMessageCache繼承了LinkedHashMap,其初始size為cacheSize * (4.0f / 3),getMessageCountAndThenIncrement方法內(nèi)部通過synchronized加鎖獲取指定msg的次數(shù),不存在則設(shè)置為0,存在則遞增;其removeEldestEntry方法判斷size() > cacheSize
小結(jié)
DuplicateMessageFilter繼承了TurboFilter,它使用了LRUMessageCache(默認(rèn)大小100)來緩存format,decide方法會執(zhí)行g(shù)etMessageCountAndThenIncrement,若超出allowedRepetitions(默認(rèn)5)則返回FilterReply.DENY,否則返回FilterReply.NEUTRAL。
以上就是logback的DuplicateMessageFilter的詳細內(nèi)容,更多關(guān)于logback的DuplicateMessageFilter的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring使用aspect注解切面不起作用的排查過程及解決
這篇文章主要介紹了spring使用aspect注解切面不起作用的排查過程及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
解決idea默認(rèn)帶的equals和hashcode引起的bug
這篇文章主要介紹了解決idea默認(rèn)帶的equals和hashcode引起的bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

