如何将模式理解为多个字段名称

How to Grok a pattern into multiple field names

如何将匹配的模式理解为多个字段名称?

是否可以使用 Grok 两次解析和分配匹配的模式?

最小的、完整的、可验证的示例

记录这行日志:

09/26/2019 Keith Miklas

应用此 grok 过滤器:

%{DATE:date}\s*%{WORD:first_name}\s*%{WORD:last_name}

这产生:

{
  "date": [
    [
      "09/26/2019"
    ]
  ],
  "first_name": [
    [
      "Keith"
    ]
  ],
  "last_name": [
    [
      "Miklas"
    ]
  ]
}

我需要的是像这样的 grok 过滤器:

%{DATE:date}\s*%{WORD:first_name,fn}\s*%{WORD:last_name,ln}
%{DATE:date}\s*%{WORD:first_name&fn}\s*%{WORD:last_name&ln}
%{DATE:date}\s*%{WORD:first_name|fn}\s*%{WORD:last_name|ln}

产生这个:

{
  "date": [
    [
      "09/26/2019"
    ]
  ],
  "first_name": [
    [
      "Keith"
    ]
  ],
  "fn": [
    [
      "Keith"
    ]
  ],
  "last_name": [
    [
      "Miklas"
    ]
  ],
  "ln": [
    [
      "Miklas"
    ]
  ]
}

您可以使用命名捕获组包装您需要的部分:

%{DATE:date}\s*(?<fn>%{WORD:first_name})\s*(?<ln>%{WORD:last_name})
               ^^^^^^                  ^   ^^^^^^                 ^

输出:

{
  "date": [
    [
      "09/26/2019"
    ]
  ],
  "fn": [
    [
      "Keith"
    ]
  ],
  "first_name": [
    [
      "Keith"
    ]
  ],
  "ln": [
    [
      "Miklas"
    ]
  ],
  "last_name": [
    [
      "Miklas"
    ]
  ]
}