diff --git a/lib/gelf/notifier.rb b/lib/gelf/notifier.rb index fcaf0de..7635d4f 100644 --- a/lib/gelf/notifier.rb +++ b/lib/gelf/notifier.rb @@ -3,11 +3,21 @@ require 'gelf/transport/tcp_tls' # replace JSON and #to_json with Yajl if available +$yajlavail = false begin - require 'yajl/json_gem' + require 'yajl' + $yajlavail = true rescue LoadError end +def json_dump(obj) + if $yajlavail + Yajl.dump(obj) + else + JSON.dump(obj) + end +end + module GELF # Graylog2 notifier. class Notifier @@ -163,7 +173,7 @@ def notify_with_level!(message_level, *args) if hash['level'] >= level if default_options['protocol'] == GELF::Protocol::TCP validate_hash(hash) - @sender.send(hash.to_json + "\0") + @sender.send(json_dump(hash) + "\0") else @sender.send_datagrams(datagrams_from_hash(hash)) end @@ -260,7 +270,7 @@ def validate_hash(hash) def serialize_hash(hash) validate_hash(hash) - Zlib::Deflate.deflate(hash.to_json).bytes + Zlib::Deflate.deflate(json_dump(hash)).bytes end def self.stringify_keys(data)