logstash自动根据预定义template生成elasticsearch日志索引相关问题

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),由于日志文件实时性要求并不是特别高,因此这里可以酌情降低频率以提高索引的写入性能;

原文地址:https://www.cnblogs.com/you-you-111/p/9844131.html