我不知道如何使用grok和logstash过滤我的日志文件

时间:2015-05-29 15:33:23

标签: elasticsearch logstash grok logstash-grok elastic-stack

我有一个小型的Java应用程序,可以加载类似于下面的日志:

Fri May 29 12:10:34 BST 2015 Trade ID: 2 status is :received
Fri May 29 14:12:36 BST 2015 Trade ID: 4 status is :received
Fri May 29 17:15:39 BST 2015 Trade ID: 3 status is :received
Fri May 29 21:19:43 BST 2015 Trade ID: 3 status is :Parsed
Sat May 30 02:24:48 BST 2015 Trade ID: 8 status is :received
Sat May 30 08:30:54 BST 2015 Trade ID: 3 status is :Data not found
Sat May 30 15:38:01 BST 2015 Trade ID: 3 status is :Book not found
Sat May 30 23:46:09 BST 2015 Trade ID: 6 status is :received

我想使用ELK堆栈来分析我的日志并过滤它们。 我想要至少3个过滤器:日期和时间,交易ID和状态。

在我的logstash配置文件的过滤器部分,这是我做的:

filter {
grok {
    match => { "message" => "%{DAY} %{MONTH} %{DAY} %{TIME} BST %{YEAR} Trade ID: %{NUMBER:tradeId}  status is : %{WORD:status}" }
  }

目前我无法按照自己的意愿过滤日志。

1 个答案:

答案 0 :(得分:0)

在模式之间有一些额外的空格,对于状态,您想要解析整个消息,因此使用GREEEDYDATA而不是WORD是您的选择。

filter {
    grok {
        match => { "message" => "%{DAY:day} %{MONTH:month} %{MONTHDAY:monthday} %{TIME:time} BST %{YEAR:year} Trade ID: %{NUMBER:tradeId} status is :%{GREEDYDATA:status}" }
    }
}

对于此日志行:

  

Sat 5月30日15:38:01 BST 2015商品编号:3状态为:未找到书籍

你最终会得到一个json:

{
   "message" => "Sat May 30 15:38:01 BST 2015 Trade ID: 3 status is :Book not found",
  "@version" => "1",
"@timestamp" => "2015-08-18T18:28:47.195Z",
      "host" => "Gabriels-MacBook-Pro.local",
       "day" => "Sat",
     "month" => "May",
  "monthday" => "30",
      "time" => "15:38:01",
      "year" => "2015",
   "tradeId" => "3",
    "status" => "Book not found"

}