メールでログ メッセージを転送するために使用している 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>
"
}
}
}