[英]Parsing in FluentD with Regexp
我们正在尝试解析我们在 AKS 群集中运行的某些服务生成的日志。 我们将 EFK 堆栈与以下版本一起使用:
Elasticsearch: 7.4.2
,FluentD: 1.7.1
,Kibana: 7.4.2
当我们在 FluentD 中使用以下配置 (json) 时,我们能够在 Kibana 仪表板中看到日志 -
<source>
@type tail
@id in_tail_container_logs
path /var/log/containers/*namespace*.log,
pos_file /var/log/fluentd-containers.log.pos
enable_stat_watcher false
tag kubernetes.*
read_from_head true
<parse>
@type json
time_format %Y-%m-%dT%H:%M:%S.%NZ
</parse>
</source>
我们需要使用正则表达式 ( regexp
) 解析日志并创建了一个。
示例日志 -
20-02-2020 08:31:42.931 [http-nio-8080-exec-1, abcd1234abcd, abcd1234abcd] INFO com.org.proj.az.controller.ModuleController.retrieveModulePortfolio - | PROJ-Module Microservice~retrieveModulePortfolio~GET~null~null~BusinessKeys[ProspectNumber:00000123456]~ Request Received for Portfolio with prospectNumber |
使用正则表达式 -
^(?<date>[^ ][^~][^abc]*)\s\[(?<threadcollection>[a-z\0-9;:,.]*)\]\s(?<log_level>\w+)\s(?<class_name>[^|]+)\|+\s(?<app_name>[^~]*)\~(?<app_operation>\w+)\~(?<http_operation>\w+)\~(?<transaction_id>[0-9a-z]+)\~(?<message_id>[0-9a-zA-Z]+)\~(?<business_keys>[^~]*)\~(?<log_message>[a-zA-Z\s:;,."']+)\|+
我们在 FluentD 中使用正则regexp
解析日志的配置 -
<source>
@type tail
@id in_tail_container_logs
path /var/log/containers/*namespace.log,
pos_file /var/log/fluentd-containers.log.pos
enable_stat_watcher false
tag kubernetes.*
read_from_head true
<parse>
@type regexp
expression /^(?<date>[^ ][^~][^abc]*)\s\[(?<threadcollection>[a-z\0-9;:,.]*)\]\s(?<log_level>\w+)\s(?<class_name>[^|]+)\|+\s(?<app_name>[^~]*)\~(?<app_operation>\w+)\~(?<http_operation>\w+)\~(?<transaction_id>[0-9a-z]+)\~(?<message_id>[0-9a-zA-Z]+)\~(?<business_keys>[^~]*)\~(?<log_message>[a-zA-Z\s:;,."']+)\|+/
time_format %Y-%m-%dT%H:%M:%S.%NZ
time_key date
keep_time_key true
</parse>
</source>
使用此方法 ( regexp
) 时,不会匹配任何日志,并且 Kibana 仪表板为空。
根据文档,您应该只使用模式本身,而不使用正则表达式定界符。
此外,如果您打算匹配数字,则不应在字符 class 中转义0
,但您必须要匹配方括号之间的任何内容,因此您需要[^\]\[]*
。
如果您不关心最后需要的捕获组之后是什么,请使用.*
,无需使用\|+
。
采用
@type regexp
expression ^(?<date>[^ ][^~][^abc]*)\s\[(?<threadcollection>[^\]\[]*)\]\s(?<log_level>\w+)\s(?<class_name>[^|]+)\|+\s(?<app_name>[^~]*)\~(?<app_operation>\w+)\~(?<http_operation>\w+)\~(?<transaction_id>[0-9a-z]+)\~(?<message_id>[0-9a-zA-Z]+)\~(?<business_keys>[^~]*)\~(?<log_message>[a-zA-Z\s:;,."']+).*
请参阅正则表达式演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.