1 回答

TA贡献1811条经验 获得超6个赞
我按照https://docs.fluentd.org/v/0.12/developer/plugin-development的说明在 ruby 中编写了一个自定义 CSV 格式化程序插件,并将文件放在路径 /etc/fluent/plugin/ 中,从而实现了这一点
require 'fluent/plugin/formatter'
module Fluent::Plugin
class MyCSVFormatter < Formatter
# Register MyCSVFormatter as 'my_csv'.
Fluent::Plugin.register_formatter('my_csv', self)
config_param :csv_fields, :array, value_type: :string
# This method does further processing. Configuration parameters can be
# accessed either via `conf` hash or member variables.
def configure(conf)
super
end
# This is the method that formats the data output.
def format(tag, time, record)
values = []
# Look up each required field and collect them from the record
@csv_fields.each do |field|
if field == "parameters"
parametervalues = []
parametervalues = record[field].split(",").map(&:strip)
values.push(*parametervalues)
next
end
v = record[field]
values << v.to_s
end
# Output by joining the fields with a comma
values.join(',') + "\n"
end
end
end
更新了 fluentd conf 文件以使用像这样的自定义格式
<label @mainstream>
<match **>
@type file
@id v6_logs
<format>
@type my_csv
csv_fields log_version,day_time,event_id,request_id,app_id,app_version,account_id,country_id,server_name,remote_ip,process_id,thread_id,item_id,message,parameters
</format>
path /fluentd/log/app.log
append true
</match>
</label>
这会产生所需的输出
6,2020-09-24 06:27:52.1826684 +0000 UTC,1700,54321,,,123467,,hostname,,1,,,Test Message(param1; param2),value1,value2
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报