Logstash提供多个插件来支持各种数据存储或搜索引擎.日志的输出事件可以发送到输出文件,标准输出或像Elasticsearch这样的搜索引擎. Logstash支持输出有三种类型,分别是&;;
标准输出
文件输出
空输出
现在让我们详细讨论这些内容.
标准输出(stdout)
它用于将过滤后的日志事件生成为命令行界面的数据流.以下是将数据库事务的总持续时间生成为stdout的示例.
logstash.conf
此配置文件包含一个stdout输出插件将总sql_duration写入标准输出.
input { file { path => "C:/tpwork/logstash/bin/log/input.log" }} filter { grok { match => [ "message", "%{LOGLEVEL:loglevel} - %{NOTSPACE:taskid} - %{NOTSPACE:logger} - %{WORD:label}( - %{INT:duration:int})?" ] } if [logger] == "TRANSACTION_START" { aggregate { task_id => "%{taskid}" code => "map['sql_duration'] = 0" map_action => "create" } } if [logger] == "SQL" { aggregate { task_id => "%{taskid}" code => "map['sql_duration'] ||= 0 ; map['sql_duration'] += event.get('duration')" } } if [logger] == "TRANSACTION_END" { aggregate { task_id => "%{taskid}" code => "event.set('sql_duration', map['sql_duration'])" end_of_task => true timeout => 120 } }}output { if [logger] == "TRANSACTION_END" { stdout { codec => line{format => "%{sql_duration}"} } }}
注意 : 如果没有安装,请安装聚合过滤器.
>logstash-plugin install Logstash-filter-aggregate
运行Logstash
我们可以使用以下命令运行Logstash.
>logstash –f logsatsh.conf
Input.log
以下代码块显示输入日志数据.
INFO - 48566 - TRANSACTION_START - startINFO - 48566 - SQL - transaction1 - 320INFO - 48566 - SQL - transaction1 - 200INFO - 48566 - TRANSACTION_END – end
stdout (它将是Windows中的命令提示符或UNIX中的终端)
这是总sql_duration 320 + 200 = 520.
520
文件输出
Logstash还可以将过滤器日志事件存储到输出文件中.我们将使用上述示例并将输出存储在文件中而不是STDOUT.
logstash.conf
此Logstash配置文件直接Logstash将总sql_duration存储到输出日志文件.
input { file { path => "C:/tpwork/logstash/bin/log/input1.log" }} filter { grok { match => [ "message", "%{LOGLEVEL:loglevel} - %{NOTSPACE:taskid} - %{NOTSPACE:logger} - %{WORD:label}( - %{INT:duration:int})?" ] } if [logger] == "TRANSACTION_START" { aggregate { task_id => "%{taskid}" code => "map['sql_duration'] = 0" map_action => "create" } } if [logger] == "SQL" { aggregate { task_id => "%{taskid}" code => "map['sql_duration'] ||= 0 ; map['sql_duration'] += event.get('duration')" } } if [logger] == "TRANSACTION_END" { aggregate { task_id => "%{taskid}" code => "event.set('sql_duration', map['sql_duration'])" end_of_task => true timeout => 120 } }}output { if [logger] == "TRANSACTION_END" { file { path => "C:/tpwork/logstash/bin/log/output.log" codec => line{format => "%{sql_duration}"} } }}
运行logstash
我们可以使用以下命令运行Logstash.
> logstash -f logsatsh.conf
input.log
以下代码块显示输入日志数据.
INFO - 48566 - TRANSACTION_START - startINFO - 48566 - SQL - transaction1 - 320INFO - 48566 - SQL - transaction1 - 200INFO - 48566 - TRANSACTION_END – end
output.log
以下代码块显示输出日志数据.
520
空输出
这是一个特殊的输出插件,用于分析输入和过滤插件的性能.