【发布时间】:2020-07-17 17:28:59
【问题描述】:
目前我正在使用 Path_Key 添加文件路径。我正在尝试获取 Path_key 中存在的多个变量。
/var/log/containers/**Application_Name**-**Application_Version**.log
是否可以从现有的字段映射中提取这些值?
【问题讨论】:
标签: parsing syslog fluentd fluent-bit
目前我正在使用 Path_Key 添加文件路径。我正在尝试获取 Path_key 中存在的多个变量。
/var/log/containers/**Application_Name**-**Application_Version**.log
是否可以从现有的字段映射中提取这些值?
【问题讨论】:
标签: parsing syslog fluentd fluent-bit
要提取用于Tag 的值,这非常简单,您可以输入如下:
[INPUT]
Name tail
Path /var/log/containers/*-*.log
Path_Key filename
Tag <appname>.<appversion>
Tag_Regex /(?<appname>[^-]+)-(?<appversion>[^.]+).log$
Tag_Regex 用于设置<appname> 和<appversion> 变量,可用于设置Tag。
至于在日志条目的字段中设置这些类型的值,我找不到任何“本机”方法来做到这一点。但是我能够通过使用Lua filter 来实现类似的目标:
[INPUT]
Name tail
Path /var/log/containers/*-*.log
Path_Key filename
[FILTER]
Name lua
Match *
script helper.lua
call extract_app_fields
调用helper.lua文件中的extract_app_fields函数:
function extract_app_fields(tag, timestamp, record)
retcode = 0
filename = record['filename']
if filename ~= nil then
appname = filename('/([^-]+)-[^.]+\.log')
appversion = filename('/[^-]+-([^.]+)\.log')
if appname ~= nil then
record['appname'] = appname
retcode = 2
end
if appversion ~= nil then
record['appversion'] = appversion
retcode = 2
end
end
return retcode, timestamp, record
end
extract_app_fields 函数从filename 中提取appname 和appversion 并更新record 中的字段(如果可以确定)。
注意:我是 Lua 新手,所以使用 Lua 可能有更好的方法。
【讨论】: