如何在发送HTTP请求之前在logstash中格式化消息

时间:2016-06-01 05:34:06

标签: regex wso2 logstash logstash-grok

我使用logstash来解析输入日志文件中的日志条目。

的logline:

TID: [0] [] [2016-05-30 23:02:02,602]  INFO {org.wso2.carbon.registry.core.jdbc.EmbeddedRegistryService} -  Configured Registry in 572ms {org.wso2.carbon.registry.core.jdbc.EmbeddedRegistryService}

Grok模式:

TID:%{SPACE}\[%{INT:SourceSystemId}\]%{SPACE}\[%{DATA:ProcessName}\]%{SPACE}\[%{TIMESTAMP_ISO8601:TimeStamp}\]%{SPACE}%{LOGLEVEL:MessageType}%{SPACE}{%{JAVACLASS:MessageTitle}}%{SPACE}-%{SPACE}%{GREEDYDATA:Message}

Grok模式运行正常。现在我想以转换的方式将此解析的输出发送到我的休息服务。

预期输出:

{
  "MessageId": "654656",
  "TimeStamp": "2001-12-31T12:00:00",
  "CorrelationId": "986565",
  "Severity": "NORMAL",
  "MessageType": "INFO",
  "MessageTitle": "TestTittle",
  "Message": "Sample Message",
  "MessageDetail": {
    "SourceSystemId": "65656",
    "ServerIP": "192.168.1.1",
    "HostName": "wedev.101",
    "ProcessId": "986",
    "ProcessName": "JAVA",
    "ThreadId": "65656",
    "MessageComponentName": "TestComponent"
  }
}

问题陈述:

我希望发送到我的休息服务的json消息应该是上面提到的格式。在logstash中我可以添加一些硬编码值并使用我通过解析得到的值日志。

以下是我的 logstash-conf 文件:

input {

    file {
        path => "C:\WSO2Environment\wso2esb-4.8.1\repository\logs\wso2carbon.log" 
        type => "wso2"
        codec =>  multiline {
            charset => "UTF-8"
            multiline_tag => "multiline"
            negate => true
            pattern => "^%{YEAR}\s%{MONTH}\s%{MONTHDAY}\s%{TIME}:\d{3}\s%{LOGLEVEL}"
            what => "previous"
        }

    }
}

filter {

    if [type] == "wso2" {
        grok {
            match => [ "message", "TID:%{SPACE}\[%{INT:SourceSystemId}\]%{SPACE}\[%{DATA:ProcessName}\]%{SPACE}\[%{TIMESTAMP_ISO8601:TimeStamp}\]%{SPACE}%{LOGLEVEL:MessageType}%{SPACE}{%{JAVACLASS:MessageTitle}}%{SPACE}-%{SPACE}%{GREEDYDATA:Message}" ]
            add_tag => [ "grokked" ]        
        }
        if !( "_grokparsefailure" in [tags] ) {
            date {
                match => [ "log_timestamp", "yyyy MMM dd HH:mm:ss:SSS" ]
                add_tag => [ "dated" ]
            }
        }
    }
    if ( "multiline" in [tags] ) {
        grok {
            match => [ "message", "Service:(?<log_service>\s[\w]+)[.\W]*Operation:(?<log_operation>\s[\w]+)" ]
            add_tag => [ "servicedetails" ]
            tag_on_failure => [ "noservicedetails" ]        
        }
    }
}

output {
#   stdout { }
     http {
        url => "http://localhost:8087/messages"
        http_method => "post"
        format => "json"
    }
}

注意:

我仍然需要配置多行格式,因此请在我的logstash配置文件中忽略该部分。

1 个答案:

答案 0 :(得分:1)

要向事件添加字段(可能包括从事件中解析的数据),您可能希望使用大多数Logstash过滤器实现的add_field功能。

最简单的方法是添加一个mutate过滤器,其中包含您想要的任何add_field函数。

mutate {
  add_field => { 
    "foo_%{somefield}" => "Hello world, from %{host}" 
  }
}

Here's the official reference