0:版本
Logstash:官方logstash:5鏡像
Elasticsearch:5.4.1
1:配置logstash.conf
Input plugin使用tcp,配置信息如下:
input {
tcp {
port => 7777
mode => server
}
}
2:日志記錄組件使用了Nlog,定制logger的target:
var target = new NetworkTarget
{
Layout = "${message}${newline}",
Address = _targetAddress
};
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, loggerLevel);
logger = LogManager.GetLogger(loggerName);
3:根據logger定義的Layout,通過logstash的filter模塊解析日志對象json:
filter {
json {
source => "message"
remove_field => [ "message" ]
}
}
這里message字符串即為nlog發送過來的日志對象json,在解析后原json字符串不再保留(remove_field的作用)
4:logstash的日志輸出落地選擇到elasticsearch,並且項目中的日志對象分了兩類,因此logstash配置的output plugin如下:
output {
if [DocumentType] == "esUserSearchLog" {
elasticsearch {
hosts => ["192.168.30.91:9200","192.168.30.92:9200"]
index => ["searchlog-%{+YYYY-MM}"]
document_type => "%{DocumentType}"
template => "/app/temp.searchlog.json"
template_name => "searchlog-*"
template_overwrite => true
}
}
if [DocumentType] == "esUserDetailLog" {
elasticsearch {
hosts => ["192.168.30.91:9200","192.168.30.92:9200"]
index => ["detaillog-%{+YYYY-MM}"]
document_type => "%{DocumentType}"
template => "/app/temp.detaillog.json"
template_name => "detaillog-*"
template_overwrite => true
}
}
}
關注:
1)DocumentType為日志對象的一個屬性,用於指定索引至ES時的document_type;
2)index配置為按月區分日志,需注意:索引名字必須對應template_name,否則將不會使用該處指定的template,此處配置的template_name為"searchlog-*",在新建所有以"searchlog-"開頭的索引將使用該template;
3)document_type指定為日志對象的DocumentType屬性即可;
4)template屬性指定對應template文件路徑(json格式,見后文)
5)template_overwrite為true則template的order高的,滿足同樣條件(如均以searchlog-開頭)的template將覆蓋order低的;
5:對應的template文件:
temp.searchlog.json:
{
"template": "searchlog-*",
"order": 1,
"settings": {
"number_of_replicas": "1",
"number_of_shards": "3",
"index": {
"refresh_interval": "10s"
}
},
...
"mappings": {
...
}
}
關注:
1)template指定的"searchlog-*"表示該template將應用於searchlog-開頭的索引的創建,並與order參數一起決定哪個模板生效(同名稱規則的order更大的模板生效);
2)setting節點下refresh_interval參數表示數據自動刷新的頻率(默認1s),由於日志文件實時性要求並不是特別高,因此這里可以酌情降低頻率以提高索引的寫入性能;
