我有一个 Logstash 配置,一直用来转发电子邮件中的日志消息。它用json
and json_encode
解析和重新编码 JSON 日志消息。
json_encode
用于漂亮地打印 JSON,这使得电子邮件看起来非常漂亮。不幸的是,随着最近的 Logstash 升级,它不再漂亮地打印。
有什么方法可以将事件的漂亮形式放入可用于电子邮件正文的字段中吗?我对 JSON、Ruby 调试或大多数其他人类可读格式很满意。
filter {
if [type] == "bunyan" {
# Save a copy of the message, in case we need to pretty-print later
mutate {
add_field => { "@orig_message" => "%{message}" }
}
json {
source => "message"
add_tag => "json"
}
}
// other filters that might add an "email" tag
if "email" in [tags] {
# pretty-print JSON for the email
if "json" in [tags] {
# re-parse the message into a field we can encode
json {
source => "@orig_message"
target => "body"
}
# encode the message, but pretty this time
json_encode {
source => "body"
target => "body"
}
}
# escape the body for HTML output
mutate {
add_field => { htmlbody => "%{body}" }
}
mutate {
gsub => [
'htmlbody', '&', '&',
'htmlbody', '<', '<'
]
}
}
}
output {
if "email" in [tags] and "throttled" not in [tags] {
email {
options => {
# config stuff...
}
body => "%{body}"
htmlbody => "
<table>
<tr><td>host:</td><td>%{host}</td></tr>
<tr><td>when:</td><td>%{@timestamp}</td></tr>
</table>
<pre>%{htmlbody}</pre>
"
}
}
}
正如 approxiblue 所说,这个问题是由 Logstash 引起的新的 JSON 解析器 https://github.com/logstash-plugins/logstash-filter-json_encode/blob/master/lib/logstash/filters/json_encode.rb(小杰克逊)。您可以使用旧解析器 https://github.com/elastic/logstash-contrib/blob/master/lib/logstash/filters/json_encode.rb作为一种解决方法,直到再次添加漂亮的打印支持。具体方法如下:
您需要更改插件 ruby 文件的两行。路径应该是这样的:
LS_HOME/vendor/bundle/jruby/1.9/gems/logstash-filter-json_encode-0.1.5/lib/logstash/filters/json_encode.rb
换线5
require "logstash/json"
into
require "json"
并换线44
event[@target] = LogStash::Json.dump(event[@source])
into
event[@target] = JSON.pretty_generate(event[@source])
就这样。重新启动 Logstash 后应该会再次漂亮地打印。
补充:
如果您不喜欢更改 ruby 源,您也可以使用 ruby 过滤器而不是 json_encode:
# encode the message, but pretty this time
ruby {
init => "require 'json'"
code => "event['body'] = JSON.pretty_generate(event['body'])"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)