2016-06-01 93 views
0

我正在使用logstash從輸入日誌文件中解析日誌條目。如何在發送HTTP請求之前在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} 

神交模式:

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

的神交模式是工作的罰款。現在我想以轉換的方式將此解析的輸出發送到我的休息服務。

預期輸出:

{ 
    "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消息應該是在上述format.Is可能在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

要向事件添加字段(可能包括從事件解析的數據),您可能需要使用大多數Logstash過濾器實現的add_field功能。

要做到這一點,最簡單的方法是通過添加一個mutate過濾器和任何你想要的add_field函數。

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

Here's the official reference

相關問題