我有一个Logstash配置,我一直使用它来转发电子邮件中的日志消息。它使用json和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>
"
}
}
}发布于 2015-08-25 16:12:07
正如approxiblue所说,这个问题是由logstash的新JSON解析器 (JrJackson)引起的。您可以使用旧解析器作为解决办法,直到再次添加漂亮打印支持为止。以下是如何:
您需要更改插件的红宝石文件的两行。路径应该类似于:
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" 转到
require "json" 换行44
event[@target] = LogStash::Json.dump(event[@source])转到
event[@target] = JSON.pretty_generate(event[@source])就这样。重新启动日志后,应该再次漂亮地打印。
补编:
如果您不喜欢更改您的红宝石源,您也可以使用红宝石过滤器而不是json_encode:
# encode the message, but pretty this time
ruby {
init => "require 'json'"
code => "event['body'] = JSON.pretty_generate(event['body'])"
}https://stackoverflow.com/questions/32077228
复制相似问题