diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0f92af8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +rvm: + - 1.8.7 + - ree + - 1.9.2 + - 1.9.3 + - jruby + +notifications: + irc: "irc.freenode.org#graylog2" diff --git a/VERSION b/VERSION index f0bb29e..3a3cd8c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.0 +1.3.1 diff --git a/gelf.gemspec b/gelf.gemspec index d3be593..9bb2784 100644 --- a/gelf.gemspec +++ b/gelf.gemspec @@ -1,53 +1,45 @@ # Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- Gem::Specification.new do |s| - s.name = %q{gelf} - s.version = "1.3.0" + s.name = "gelf" + s.version = "1.3.1" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Alexey Palazhchenko", "Lennart Koopmann"] - s.date = %q{2011-07-27} - s.description = %q{Library to send GELF messages to Graylog2 logging server. Supports plain-text, GELF messages and exceptions.} - s.email = %q{alexey.palazhchenko@gmail.com} + s.date = "2011-10-28" + s.description = "Library to send GELF messages to Graylog2 logging server. Supports plain-text, GELF messages and exceptions." + s.email = "alexey.palazhchenko@gmail.com" s.extra_rdoc_files = [ "LICENSE", - "README.rdoc" + "README.rdoc" ] s.files = [ - ".gitignore", - "CHANGELOG", - "LICENSE", - "README.rdoc", - "Rakefile", - "VERSION", - "benchmarks/notifier.rb", - "gelf.gemspec", - "lib/gelf.rb", - "lib/gelf/logger.rb", - "lib/gelf/notifier.rb", - "lib/gelf/ruby_sender.rb", - "lib/gelf/severity.rb", - "test/helper.rb", - "test/test_logger.rb", - "test/test_notifier.rb", - "test/test_ruby_sender.rb", - "test/test_severity.rb" - ] - s.homepage = %q{http://github.com/Graylog2/gelf-rb} - s.rdoc_options = ["--charset=UTF-8"] - s.require_paths = ["lib"] - s.rubygems_version = %q{1.4.1} - s.summary = %q{Library to send GELF messages to Graylog2 logging server.} - s.test_files = [ + ".travis.yml", + "CHANGELOG", + "LICENSE", + "README.rdoc", + "Rakefile", + "VERSION", + "benchmarks/notifier.rb", + "gelf.gemspec", + "lib/gelf.rb", + "lib/gelf/logger.rb", + "lib/gelf/notifier.rb", + "lib/gelf/ruby_sender.rb", + "lib/gelf/severity.rb", + "test/helper.rb", + "test/test_logger.rb", + "test/test_notifier.rb", "test/test_ruby_sender.rb", - "test/test_logger.rb", - "test/test_notifier.rb", - "test/test_severity.rb", - "test/helper.rb" + "test/test_severity.rb" ] + s.homepage = "http://github.com/Graylog2/gelf-rb" + s.require_paths = ["lib"] + s.rubygems_version = "1.8.11" + s.summary = "Library to send GELF messages to Graylog2 logging server." if s.respond_to? :specification_version then s.specification_version = 3 diff --git a/lib/gelf/notifier.rb b/lib/gelf/notifier.rb index bbe3160..29afc0f 100644 --- a/lib/gelf/notifier.rb +++ b/lib/gelf/notifier.rb @@ -6,7 +6,7 @@ class << self attr_accessor :last_chunk_id end - attr_accessor :enabled, :collect_file_and_line + attr_accessor :enabled, :collect_file_and_line, :rescue_network_errors attr_reader :max_chunk_size, :level, :default_options, :level_mapping # +host+ and +port+ are host/ip and port of graylog2-server. @@ -18,6 +18,7 @@ def initialize(host = 'localhost', port = 12201, max_size = 'WAN', default_optio self.level = GELF::DEBUG self.max_chunk_size = max_size + self.rescue_network_errors = false self.default_options = default_options self.default_options['version'] = SPEC_VERSION @@ -128,6 +129,8 @@ def #{const.downcase}(*args) # def debug(*args) private def notify_with_level(message_level, *args) notify_with_level!(message_level, *args) + rescue SocketError + raise unless self.rescue_network_errors rescue Exception => exception notify_with_level!(GELF::UNKNOWN, exception) end @@ -203,7 +206,7 @@ def datagrams_from_hash data = serialize_hash datagrams = [] - # Maximum total size is 8192 byte for UDP datagram. Split to chunks if bigger. (GELFv2 supports chunking) + # Maximum total size is 8192 byte for UDP datagram. Split to chunks if bigger. (GELF v1.0 supports chunking) if data.count > @max_chunk_size id = self.class.last_chunk_id += 1 msg_id = Digest::MD5.digest("#{Time.now.to_f}-#{id}")[0, 8] diff --git a/test/test_notifier.rb b/test/test_notifier.rb index ba6fb59..01b253d 100644 --- a/test/test_notifier.rb +++ b/test/test_notifier.rb @@ -120,19 +120,18 @@ class TestNotifier < Test::Unit::TestCase end should "set timestamp to specified time" do - date = Time.now.utc-9001.to_f - hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message', 'timestamp' => date }) - assert_equal date, hash['timestamp'] + timestamp = 1319799449.13765 + hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message', 'timestamp' => timestamp }) + assert_equal timestamp, hash['timestamp'] end end context "serialize_hash" do setup do @notifier.level_mapping = :direct - @date = Time.now.utc-9001.to_f - @notifier.instance_variable_set('@hash', { 'level' => GELF::WARN, 'field' => 'value', 'timestamp' => @date }) + @notifier.instance_variable_set('@hash', { 'level' => GELF::WARN, 'field' => 'value' }) @data = @notifier.__send__(:serialize_hash) - assert @data.respond_to?(:each) + assert @data.respond_to?(:each) # Enumerable::Enumerator in 1.8, ::Enumerator in 1.9, so... @deserialized_hash = JSON.parse(Zlib::Inflate.inflate(@data.to_a.pack('C*'))) assert_instance_of Hash, @deserialized_hash end @@ -141,7 +140,6 @@ class TestNotifier < Test::Unit::TestCase assert_not_equal GELF::WARN, @deserialized_hash['level'] assert_not_equal GELF::LOGGER_MAPPING[GELF::WARN], @deserialized_hash['level'] assert_equal GELF::DIRECT_MAPPING[GELF::WARN], @deserialized_hash['level'] - assert_equal @date.to_s, @deserialized_hash['timestamp'] end end @@ -243,4 +241,19 @@ class TestNotifier < Test::Unit::TestCase assert_nothing_raised { @notifier.notify(:no_short_message => 'too bad') } end end + + context "with notifier with real sender" do + setup do + @notifier = GELF::Notifier.new('no_such_host_321') + end + + should "raise exception" do + assert_raise(SocketError) { @notifier.notify('Hello!') } + end + + should "not raise exception if asked" do + @notifier.rescue_network_errors = true + assert_nothing_raised { @notifier.notify('Hello!') } + end + end end