logback ThresholdFilter臨界值日志過濾器源碼解讀
序
本文主要研究一下logback的ThresholdFilter
ThresholdFilter
ch/qos/logback/classic/filter/ThresholdFilter.java
public class ThresholdFilter extends Filter<ILoggingEvent> {
Level level;
@Override
public FilterReply decide(ILoggingEvent event) {
if (!isStarted()) {
return FilterReply.NEUTRAL;
}
if (event.getLevel().isGreaterOrEqual(level)) {
return FilterReply.NEUTRAL;
} else {
return FilterReply.DENY;
}
}
public void setLevel(String level) {
this.level = Level.toLevel(level);
}
public void start() {
if (this.level != null) {
super.start();
}
}
}ThresholdFilter繼承了Filter,其decide方法從ILoggingEvent獲取level,若該level大于等于指定的level則返回NEUTRAL,否則返回DENY
isGreaterOrEqual
ch/qos/logback/classic/Level.java
public final class Level implements java.io.Serializable {
private static final long serialVersionUID = -814092767334282137L;
public static final int OFF_INT = Integer.MAX_VALUE;
public static final int ERROR_INT = 40000;
public static final int WARN_INT = 30000;
public static final int INFO_INT = 20000;
public static final int DEBUG_INT = 10000;
public static final int TRACE_INT = 5000;
public static final int ALL_INT = Integer.MIN_VALUE;
/**
* The <code>OFF</code> is used to turn off logging.
*/
public static final Level OFF = new Level(OFF_INT, "OFF");
/**
* The <code>ERROR</code> level designates error events which may or not be
* fatal to the application.
*/
public static final Level ERROR = new Level(ERROR_INT, "ERROR");
/**
* The <code>WARN</code> level designates potentially harmful situations.
*/
public static final Level WARN = new Level(WARN_INT, "WARN");
/**
* The <code>INFO</code> level designates informational messages highlighting
* overall progress of the application.
*/
public static final Level INFO = new Level(INFO_INT, "INFO");
/**
* The <code>DEBUG</code> level designates informational events of lower
* importance.
*/
public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
/**
* The <code>TRACE</code> level designates informational events of very low
* importance.
*/
public static final Level TRACE = new Level(TRACE_INT, "TRACE");
/**
* The <code>ALL</code> is used to turn on all logging.
*/
public static final Level ALL = new Level(ALL_INT, "ALL");
public final int levelInt;
public final String levelStr;
/**
* Instantiate a Level object.
*/
private Level(int levelInt, String levelStr) {
this.levelInt = levelInt;
this.levelStr = levelStr;
}
/**
* Returns <code>true</code> if this Level has a higher or equal Level than the
* Level passed as argument, <code>false</code> otherwise.
*/
public boolean isGreaterOrEqual(Level r) {
return levelInt >= r.levelInt;
}
//......
}Level定義了OFF(Integer.MAX_VALUE)、ERROR(40000)、WARN(30000)、INFO(20000)、DEBUG(10000)、TRACE(5000)、ALL(Integer.MIN_VALUE)這幾個(gè)level,其int值依次減小。isGreaterOrEqual方法則是根據(jù)levelInt值來判斷。
示例
<appender name="FILE_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>infoLog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>infoLogs.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>這里對(duì)FILE_INFO添加了ThresholdFilter,只有級(jí)別大于等于INFO的才打印
小結(jié)
logback的ThresholdFilter繼承了Filter,其decide方法從ILoggingEvent獲取level,若該level大于等于指定的level則返回NEUTRAL,否則返回DENY。Level定義了OFF(Integer.MAX_VALUE)、ERROR(40000)、WARN(30000)、INFO(20000)、DEBUG(10000)、TRACE(5000)、ALL(Integer.MIN_VALUE)這幾個(gè)level,其int值依次減小。isGreaterOrEqual方法則是根據(jù)levelInt值來判斷。
以上就是logback ThresholdFilter臨界值日志過濾器源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于logback ThresholdFilter日志過濾器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot實(shí)現(xiàn)嵌入式 Servlet容器
傳統(tǒng)的Spring MVC工程部署時(shí)需要將WAR文件放置在servlet容器的文檔目錄內(nèi),而Spring Boot工程使用嵌入式servlet容器省去了這一步驟,本文就來設(shè)置一下相關(guān)配置,感興趣的可以了解一下2023-12-12
Collections工具類_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Collections工具類提供了大量針對(duì)Collection/Map的操作。這篇文章主要介紹了Collections工具類_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-04-04
Spring注解驅(qū)動(dòng)之BeanFactoryPostProcessor原理解析
這篇文章主要介紹了Spring注解驅(qū)動(dòng)之BeanFactoryPostProcessor原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
java 中cookie的詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了java 中cookie的詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,這里對(duì)cookie 的建立與讀取,和設(shè)定cookie 生命周期等詳細(xì)介紹,需要的朋友可以參考下2017-01-01
在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
隨著微服務(wù)架構(gòu)的流行,服務(wù)之間的調(diào)用變得越來越頻繁和復(fù)雜,流量控制是保障系統(tǒng)穩(wěn)定性的重要手段之一,它可以幫助我們避免因過載而導(dǎo)致的服務(wù)不可用,本文將介紹如何在Spring Boot項(xiàng)目中使用Spring Cloud Sentinel來實(shí)現(xiàn)流量控制,需要的朋友可以參考下2024-08-08
spring?webClient配置及使用簡(jiǎn)單代碼示例
WebClient是Spring框架5.0引入的基于響應(yīng)式編程模型的HTTP客戶端,它提供一種簡(jiǎn)便的方式來處理HTTP請(qǐng)求和響應(yīng),支持異步和非阻塞式的請(qǐng)求和響應(yīng)處理,下面這篇文章主要給大家介紹了關(guān)于spring?webClient配置及使用的相關(guān)資料,需要的朋友可以參考下2024-03-03
Spring?JPA的實(shí)體屬性類型轉(zhuǎn)換器并反序列化工具類詳解
這篇文章主要介紹了Spring?JPA的實(shí)體屬性類型轉(zhuǎn)換器并反序列化工具類詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

