2016-06-28 667 views
0

我为此问题搜索了一个解决方案,并添加了Ruby代码。问题是如果我使用gsub我也遇到错误。Logstash - 为json转义字符添加执行

我需要一个解决方案,以解决Ruby错误或其他方法来为转义字符添加例外。

这里是我的Logstash过滤器:

filter { 
    if [type] == "apache" { 
    json { 
      source => "message" 
     } 
    ruby { 
      code => "str=event['url']; str=str.gsub('\\','\*'); event['url']=str;" 
     } 
} 
} 

我得到我的日志与Apache的JSON格式。这里是Logstash错误信息:

{:timestamp=>"2016-06-28T12:49:21.821000+0100", :message=>"Error 
parsing json", :source=>"message", :raw=>"{ \"@version\": \"1\", 
\"@timestamp\": \"2016-06-28T12:49:16.000+0100\", \"message\": \"GET 
/yolo/q-ap%C3%Ablah/?search%5Bdist%5D=15&page=13 HTTP/1.1\", \"via\": 
\"192.168.220.100\", \"client-ip\": \"123.123.123.123\", 
\"remote-logname\": \"-\", \"remote-user\": \"-\", \"recv-time\": 
\"[28/Jun/2016:12:49:16 +0100]\", \"serve-time-microsec\": \"88613\", 
\"request\": \"GET /yolo/q-ap%C3%Ablah/?search%5Bdist%5D=15&page=13 
HTTP/1.1\", \"status\": \"200\", \"size\": \"184985\", \"referer\": 
\"-\", \"user-agent\": \"Mozilla/5.0 (compatible; Googlebot/2.1; 
+http://www.google.com/bot.html)\", \"url\": \"/yolo/q-ap\\xc3\\xablah/\", \"query\": 
\"?search%5Bdist%5D=15&page=13\", \"method\": \"GET\", \"protocol\": 
\"HTTP/1.1\", \"vhost\": \"www.site.com\", \"received-size\": \"1136\" 
}", :exception=>#LogStash::Json::ParserError: Unrecognized character 
escape 'x' (code 120) at Source: [[email protected]; line: 1, column: 599>, 
:level=>:warn} 

{:timestamp="2016-06-28T12:49:21.821000+0100", :message=>"Ruby 
exception occurred: undefined method `gsub' for nil:NilClass", 
:level=>:error} 
+0

我不能完全确定,但也许有一些待办事项与您的代码是内部的一个字符串 - >“< - 然后在您的gsub,尽管你使用了 - >'< - 你有一个\ Normaly这会转义下面的char。你可以在这里使用一些转义吗? – Shimu

+0

原始的ruby代码如下所示:'code =>'str = event [ request_body“]; str = str.gsub(”\\ x22“,”\“”)。gsub(“\\ x5C”,“\\”);事件[“request_body”] = str;''我得到完全相同的错误信息。 – Infinite

回答