在logback.xml中自定義動態(tài)屬性的方法
當(dāng)使用logback來記錄Web應(yīng)用的日志時,我們通過在logback.xml中配置appender來指定日志輸出格式及輸出文件路徑,這在一臺主機或一個文件系統(tǒng)上部署單個實例沒有問題,但是如果部署多個實例(比如通過容器的方式),多個實例同時往同一文件寫日志可能就會引起問題。這時可以將每個實例的日志文件加以區(qū)分,如IP或UUID,或兩者結(jié)合的形式。這其實就涉及如何在logback.xml中自定義動態(tài)屬性的問題。
可以有4種方式來實現(xiàn)logback.xml中獲取自定義變量值:
- 通過設(shè)置環(huán)境變量或傳遞系統(tǒng)屬性(比如在程序啟動時通過-D傳遞)的方式,兩者是可以直接在logback.xml中通過 ${變量名} 獲取的。
- 自定義logback.xml的加載時機,在其加載前將需要設(shè)置的屬性注入到logback的context中,這種方式相對復(fù)雜,本文不討論。
- 通過實現(xiàn)PropertyDefiner接口來提供屬性值設(shè)置
- 通過實現(xiàn)LoggerContextListener接口來設(shè)置屬性值
第一種方式簡單,但不能通過程序生成屬性值,第二種方式稍顯復(fù)雜,本文主要介紹后兩種方式。
PropertyDefiner方式
首先定義一個類,實現(xiàn)PropertyDefiner接口,可以通過繼承PropertyDefinerBase會更方便
import ch.qos.logback.core.PropertyDefinerBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; /*** * 將本地IP拼接到日志文件名中,以區(qū)分不同實例,避免存儲到同一位置時的覆蓋沖突問題 * @Author ronwxy * @Date 2019/8/20 16:17 */ public class IPLogDefiner extends PropertyDefinerBase { private static final Logger LOG = LoggerFactory.getLogger(IPLogDefiner.class); private String getUniqName() { String localIp = null; try { localIp = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { LOG.error("fail to get ip...", e); } String uniqName = UUID.randomUUID().toString().replace("-", ""); if (localIp != null) { uniqName = localIp + "-" + uniqName; } return uniqName; } @Override public String getPropertyValue() { return getUniqName(); } }
然后在logback.xml中,添加 <define>
配置,指定屬性名(本例中為localIP)及獲取屬性值的實現(xiàn)類,這樣就可以在配置中通過 ${localIP}
來引用該屬性值了。在實現(xiàn)方法 getPropertyValue
中返回你需要生成的值,本例中是返回 本地IP-UUID 的形式。
<configuration> <define name="localIP" class="cn.jboost.common.IPLogDefiner"/> <appender name="interfaceLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <File>D:\\logs\\elk\\interface-${localIP}.log</File> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> # 省略了其它配置
LoggerContextListener方式
定義一個實現(xiàn)LoggerContextListener接口的類,在start方法中,將需要設(shè)置的屬性設(shè)置到logback的Context中,
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.LoggerContextListener; import ch.qos.logback.core.Context; import ch.qos.logback.core.spi.ContextAwareBase; import ch.qos.logback.core.spi.LifeCycle; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; /*** * 第二種實現(xiàn)方式 * @Author ronwxy * @Date 2019/8/20 18:45 */ public class LoggerStartupListener extends ContextAwareBase implements LoggerContextListener, LifeCycle { private boolean started = false; @Override public void start() { if (started) { return; } Context context = getContext(); context.putProperty("localIP", getUniqName()); started = true; } private String getUniqName() { String localIp = null; try { localIp = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { //LOG.error("fail to get ip...", e); } String uniqName = UUID.randomUUID().toString().replace("-", ""); if (localIp != null) { uniqName = localIp + "-" + uniqName; } return uniqName; } //省略了其它函數(shù)
然后在logback.xml中,配置如上監(jiān)聽器類,這樣就可以通過 ${localIP} 獲取到上面 context.putProperty("localIP", getUniqName()); 設(shè)置的值了。
<configuration> <!--<define name="localIP" class="com.cnbot.common.IPLogDefiner"/>--> <contextListener class="cn.jboost.common.LoggerStartupListener"/> <define name="localIP" class="com.cnbot.common.IPLogDefiner"/> <appender name="interfaceLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <File>D:\\logs\\elk\\interface-${localIP}.log</File> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> # 省略了其它配置
這種方式能設(shè)置任意個數(shù)的屬性值,比前一種方式靈活。
總結(jié)
在logback.xml中獲取自定義屬性值,主要是需要在加載前將對應(yīng)的屬性值進行設(shè)置,這樣加載時才能有效獲取。本文雖是自定義日志文件名稱,但不局限于此,所有需要動態(tài)獲取的變量都可以按這種方式實現(xiàn)。
相關(guān)文章
springboot下添加日志模塊和設(shè)置日志文件輸出的方法
日志的使用將通過SLF4J來使用,SLF4J是一個為Java應(yīng)用提供簡單日志記錄的接口,在Spring框架中,SLF4J常常用于處理框架本身以及應(yīng)用程序的日志記錄,本文給大家介紹springboot下添加日志模塊和設(shè)置日志文件輸出的相關(guān)知識,感興趣的朋友一起看看吧2023-12-12淺談Java內(nèi)部類——靜態(tài)內(nèi)部類
這篇文章主要介紹了Java靜態(tài)內(nèi)部類的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java內(nèi)部類的相關(guān)知識,感興趣的朋友可以了解下2020-08-08Java實現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle 的相關(guān)資料,需要的朋友可以參考下2016-06-06SpringBoot-RestTemplate如何實現(xiàn)調(diào)用第三方API
這篇文章主要介紹了SpringBoot-RestTemplate實現(xiàn)調(diào)用第三方API的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08MyBatis中使用分頁插件PageHelper實現(xiàn)分頁功能
分頁是經(jīng)常使用的功能,本文主要介紹了Mybatis中處理特殊SQL處理邏輯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Mybatis-plus的selectPage()分頁查詢不生效問題解決
本文主要介紹了Mybatis-plus的selectPage()分頁查詢不生效問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01