日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot(2.2.4)配置druid的log4j2日志监控

發(fā)布時間:2024/9/30 编程问答 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot(2.2.4)配置druid的log4j2日志监控 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

druid連接池標(biāo)榜自己是唯一有監(jiān)控的連接池,性能最好的連接池。其中一項(xiàng)是強(qiáng)大的日志功能,方便程序員定位問題,配置log4j2步驟如下:

1.去除springboot默認(rèn)的logback日志框架,請參看我的另一篇博客https://blog.csdn.net/weixin_39370859/article/details/105039787

2.配置數(shù)據(jù)源(我用的是配置文件的方式,也可以代碼里注解引入的方式),在resource下建相應(yīng)的目錄和spring的bean文件,比如我的

再在啟動類里引入

?接下來是druidBeans.xml的配置

<!--name屬性,配置這個屬性的意義在于,如果存在多個數(shù)據(jù)源,監(jiān)控的時候可以通過名字來區(qū)分開來。如果沒有配置,將會生成一個名字,格式是:"DataSource-" + System.identityHashCode(this). 另外配置此屬性至少在1.0.5版本中是不起作用的,強(qiáng)行設(shè)置name會出錯--><bean id="dataSource" name="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close"><!--用戶名、密碼,url等其他 property的配置不寫出來了--><!--類型是List<com.alibaba.druid.filter.Filter>,如果同時配置了filters和proxyFilters,是組合關(guān)系,并非替換關(guān)系--><property name="proxyFilters"><list><ref bean="log-filter"/></list></property></bean><bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4j2Filter"><!--所有DataSource相關(guān)的日志--><!--<property name="dataSourceLogEnabled" value="true"/>--><!--所有連接相關(guān)的日志--><!--<property name="connectionLogEnabled" value="true"/>--><!--所有連接上發(fā)生異常的日志--><!--<property name="connectionLogErrorEnabled" value="true"/>--><!--所有Statement相關(guān)的日志--><!--<property name="statementLogEnabled" value="true"/>--><!--所有Statement發(fā)生異常的日志--><!--<property name="statementLogErrorEnabled" value="true"/>--><!--所有resultSet的日志--><!--<property name="resultSetLogEnabled" value="true"/>--><!--resultSet發(fā)生異常的日志--><!--<property name="resultSetLogErrorEnabled" value="true"/>--><!--數(shù)據(jù)庫連接之前的日志--><!--<property name="connectionConnectBeforeLogEnabled" value="true"/>--><!--數(shù)據(jù)庫連接后的日志--><!--<property name="connectionConnectAfterLogEnabled" value="true"/>--><!--提交之后的額日志--><!--<property name="connectionCommitAfterLogEnabled" value="true"/>--><!--連接回滾之后的日志--><!--<property name="connectionRollbackAfterLogEnabled" value="true"/>--><!--連接關(guān)閉之后的日志--><!--<property name="connectionCloseAfterLogEnabled" value="true"/>--><!--Statement創(chuàng)建之后的日志--><!--<property name="statementCreateAfterLogEnabled" value="true"/>--><!--StatementPrepared創(chuàng)建之后的日志--><!--<property name="statementPrepareAfterLogEnabled" value="true"/>--><!--StatementPrepare調(diào)用之后的日志--><!--<property name="statementPrepareCallAfterLogEnabled" value="true"/>--><!--StatementPrepare執(zhí)行之后的日志--><!--<property name="statementExecuteAfterLogEnabled" value="true"/>--><!--Statement執(zhí)行查詢之后的日志--><!--<property name="statementExecuteQueryAfterLogEnabled" value="true"/>--><!--Statement執(zhí)行更新之后的日志--><!--<property name="statementExecuteUpdateAfterLogEnabled" value="true"/>--><!--Statement執(zhí)行批量之后的日志--><!--<property name="statementExecuteBatchAfterLogEnabled" value="true"/>--><!--Statement執(zhí)行關(guān)閉之后的日志--><!--<property name="statementCloseAfterLogEnabled" value="true"/>--><!--參數(shù)日志--><!--<property name="statementParameterSetLogEnabled" value="true"/>--><!--result相關(guān)的日志--><!--<property name="resultSetNextAfterLogEnabled" value="true"/>--><!--<property name="resultSetOpenAfterLogEnabled" value="true"/>--><!--<property name="resultSetCloseAfterLogEnabled" value="true"/>--></bean>

?log4j2監(jiān)控可以以

<property name="filters" value="log4j2"/>

的方式引入,這個filters是過濾器列表,value里以逗號隔開,這里的log4j2是一個別名,是默認(rèn)的,阿里的文檔里有說明,filters的引入方法和proxyFilters的引入是組合關(guān)系,兩者引入同一種filter的情況沒測過,不是同一種filter的就兩種都有效,用proxyFilters的好處是可以自己定制里邊的屬性。配置到這里就完事了。

3.效果

監(jiān)控日志輸出在你配置的log4j2.xml里的文件類型appender的路徑里,沒有改動的話在項(xiàng)目根路徑下回生成logs文件夾,里頭應(yīng)包含文件列表:

其中druid-sql.log里頭根據(jù)配置的屬性可以記錄連接開始時間、statement時間、sql參數(shù)、類型、sql語句、執(zhí)行用時、statement銷毀等?

?

?

總結(jié)

以上是生活随笔為你收集整理的springboot(2.2.4)配置druid的log4j2日志监控的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。