Spring Cloud 实现分布式实时日志分析采集的三种方案( 二 )

(1)Logstash中配置的what属性值为previous , 相当于Filebeat中的after,Logstash中配置的what属性值为next,相当于Filebeat中的before 。
(2)pattern => "%{LOGLEVEL}s*]" 中的LOGLEVEL是Logstash预制的正则匹配模式 , 预制的还有好多常用的正则匹配模式,详细请看:https://Github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns
2. 问题:如何将Kibana中显示日志的时间字段替换为日志信息中的时间?默认情况下,我们在Kibana中查看的时间字段与日志信息中的时间不一致,因为默认的时间字段值是日志收集时的当前时间 , 所以需要将该字段的时间替换为日志信息中的时间 。
解决方案:使用grok分词插件与date时间格式化插件来实现
在Logstash的配置文件的过滤器中配置grok分词插件与date时间格式化插件,如:
input {beats {port => 5044}}filter {multiline {pattern => "%{LOGLEVEL}s*][%{YEAR}%{MONTHNUM}%{MONTHDAY}s+%{TIME}]"negate => truewhat => "previous"}grok { match => [ "message" , "(?<customer_time>%{YEAR}%{MONTHNUM}%{MONTHDAY}s+%{TIME})" ]}date {match => ["customer_time", "yyyyMMdd HH:mm:ss,SSS"] //格式化时间target => "@timestamp" //替换默认的时间字段}}output {elasticsearch {hosts => "localhost:9200"}}如要匹配的日志格式为:[DEBUG][20170811 10:07:31,359][DefaultBeanDefinitionDocumentReader:106] Loading bean definitions,解析出该日志的时间字段的方式有:
① 通过引入写好的表达式文件,如表达式文件为customer_patterns , 内容为:CUSTOMER_TIME %{YEAR}%{MONTHNUM}%{MONTHDAY}s+%{TIME}
注:内容格式为:[自定义表达式名称] [正则表达式]
然后logstash中就可以这样引用:
filter {grok {patterns_dir => ["./customer-patterms/mypatterns"] //引用表达式文件路径match => [ "message" , "%{CUSTOMER_TIME:customer_time}" ] //使用自定义的grok表达式}}② 以配置项的方式,规则为:(?<自定义表达式名称>正则匹配规则),如:
filter {grok {match => [ "message" , "(?<customer_time>%{YEAR}%{MONTHNUM}%{MONTHDAY}s+%{TIME})" ]}}3. 问题:如何在Kibana中通过选择不同的系统日志模块来查看数据一般在Kibana中显示的日志数据混合了来自不同系统模块的数据,那么如何来选择或者过滤只查看指定的系统模块的日志数据?
解决方案:新增标识不同系统模块的字段或根据不同系统模块建ES索引
1、新增标识不同系统模块的字段,然后在Kibana中可以根据该字段来过滤查询不同模块的数据,这里以第二种部署架构讲解,在Filebeat中的配置内容为:
filebeat.prospectors:-paths:- /home/project/elk/logs/account.loginput_type: logmultiline:pattern: '^['negate: truematch: afterfields: //新增log_from字段log_from: account-paths:- /home/project/elk/logs/customer.loginput_type: logmultiline:pattern: '^['negate: truematch: afterfields:log_from: customeroutput:logstash:hosts: ["localhost:5044"]通过新增:log_from字段来标识不同的系统模块日志
2、根据不同的系统模块配置对应的ES索引,然后在Kibana中创建对应的索引模式匹配,即可在页面通过索引模式下拉框选择不同的系统模块数据 。
filebeat.prospectors:-paths:- /home/project/elk/logs/account.loginput_type: logmultiline:pattern: '^['negate: truematch: afterfields: //新增log_from字段log_from: account-paths:- /home/project/elk/logs/customer.loginput_type: logmultiline:pattern: '^['negate: truematch: afterfields:log_from: customeroutput:logstash:hosts: ["localhost:5044"]这里以第二种部署架构讲解,分为两步:
① 在Filebeat中的配置内容为:
filebeat.prospectors:-paths:- /home/project/elk/logs/account.loginput_type: logmultiline:pattern: '^['negate: truematch: afterdocument_type: account-paths:- /home/project/elk/logs/customer.loginput_type: logmultiline:pattern: '^['negate: truematch: afterdocument_type: customeroutput:logstash:hosts: ["localhost:5044"]通过document_type来标识不同系统模块
② 修改Logstash中output的配置内容为:
output {elasticsearch {hosts => "localhost:9200"index => "%{type}"}}


在output中增加index属性,%{type}表示按不同的document_type值建ES索引

总结本文主要介绍了ELK实时日志分析的三种部署架构,以及不同架构所能解决的问题 , 这三种架构中第二种部署方式是时下最流行也是最常用的部署方式 。


推荐阅读