默認情況,慢日志是不開啟的。要開啟它,需要定義具體動作(query,fetch 還是 index),你期望的事件記錄等級( WARN、INFO
、DEBUG、TRACE
等),以及時間閾值。
es有幾種搜索模式,比如 query_then_fetch , 表示先從各個節點query到id,然后整合,再去各個節點拿具體數據
這是一個索引級別的設置,也就是說可以獨立應用給單個或所有索引,這個配置是永久的,配置后即使集群重啟也會保留
PUT /_all/_settings { "index.search.slowlog.threshold.query.warn":"5s", "index.search.slowlog.threshold.query.info":"2s", "index.search.slowlog.threshold.query.debug":"1s", "index.search.slowlog.threshold.query.trace":"400ms", "index.search.slowlog.threshold.fetch.warn":"1s", "index.search.slowlog.threshold.fetch.info":"800ms", "index.search.slowlog.threshold.fetch.debug":"500ms", "index.search.slowlog.threshold.fetch.trace":"200ms", "index.indexing.slowlog.threshold.index.warn":"5s", "index.indexing.slowlog.threshold.index.info":"2s", "index.indexing.slowlog.threshold.index.debug":"1s", "index.indexing.slowlog.threshold.index.trace":"400ms" }
查詢慢於 5 秒輸出一個 WARN 日志
索引慢於 2 秒輸出一個 INFO 日志
獲取慢於 1 秒輸出一個 DEBUG 日志
參考資料:https://www.elastic.co/guide/cn/elasticsearch/guide/current/logging.html
兩種開啟方式:
一、通過修改elasticsearch.yml來啟用慢查詢:
vim elasticsearch.yml
###Search Slow Log :查詢慢日志配置,日志記錄在以“_index_isearch_slowlog.log” 結尾的文件中
#注:配置不一定都需要,自己選擇需要那種級別(warn、info、debug、trace)日志,關閉的話配置成-1 就可以了,注釋掉重啟也可以
index.search.slowlog.threshold.query.warn: 10s #超過10秒的query產生1個warn日志
index.search.slowlog.threshold.query.info: 5s #超過5秒的query產生1個info日志
index.search.slowlog.threshold.query.debug: 2s #超過2秒的query產生1個debug日志
index.search.slowlog.threshold.query.trace: 500ms #超過500毫秒的query產生1個trace日志
index.search.slowlog.threshold.fetch.warn: 1s ##fetch階段的配置
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug: 500ms
index.search.slowlog.threshold.fetch.trace: 200ms
###Index Slow log:索引慢日志配置 ,日志記錄在以“_index_indexing_slowlog.log” 結尾的文件中
#注:上邊四個配置不一定都需要,自己選擇需要那種級別(warn、info、debug、trace)日志關閉的話配置成-1就可以了
index.indexing.slowlog.threshold.index.warn: 10s ##索引數據超過10秒產生一個warn日志
index.indexing.slowlog.threshold.index.info: 5s ##索引數據超過5秒產生一個info日志
index.indexing.slowlog.threshold.index.debug: 2s ##索引數據超過2秒產生一個ddebug日志
index.indexing.slowlog.threshold.index.trace: 500ms ##索引數據超過500毫秒產生一個trace日志
二、通過API動態的修改配置:
這是一個索引級別的設置,也就是說可以獨立應用給單個索引:這個配置是永久的,配置后即使集群重啟也會保留。如果關閉日志記錄的話將選項修改成 -1 即可(例如: "index.search.slowlog.threshold.query.warn" : -1
PUT /my_index/_settings
{
"index.search.slowlog.threshold.query.warn" : "10s",
"index.search.slowlog.threshold.fetch.debug": "500ms",
"index.indexing.slowlog.threshold.index.info": "5s"
}
查詢慢於 10 秒輸出一個 WARN 日志。
獲取慢於 500 毫秒輸出一個 DEBUG 日志。
索引慢於 5 秒輸出一個 INFO 日志。
這是一個集群級別的設置:一旦閾值設置過了(可以在 elasticsearch.yml 文件里定義這些閾值。沒有閾值設置的索引會自動繼承在靜態配置文件里配置的參數),你可以和其他日志器一樣切換日志記錄等級。這個API簡單試了下,沒效果。並沒有改變日志記錄級別。而且我沒找到集群級別的設置慢查詢閾值的API。有知道的發個鏈接(QQ:1250134974)
PUT /_cluster/settings
{
"transient" : {
"logger.index.search.slowlog" : "DEBUG",
"logger.index.indexing.slowlog" : "WARN"
}
}
設置搜索慢日志為 DEBUG 級別。
設置索引慢日志為 WARN 級別。