我有一个 Logstash 配置,我一直在使用它来转发电子邮件中的日志消息。它使用json
andjson_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>
"
}
}
}