From abdc8c6af908f09e14bcee9cbb6a4103e5232201 Mon Sep 17 00:00:00 2001 From: Sascha Hoellger Date: Fri, 25 May 2012 11:24:58 +0200 Subject: [PATCH 01/23] Add rails log tag support. --- lib/gelf/logger.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index f32b58b..4a76f1a 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -22,6 +22,12 @@ def add(level, *args) hash = {'short_message' => message, 'facility' => progname} hash.merge!(self.class.extract_hash_from_exception(message)) if message.is_a?(Exception) + if default_options['tags'] + tags = current_tags + default_options['tags'].each_with_index do |tag_name, index| + hash.merge!("_#{tag_name}" => tags[index]) if tags[index] + end + end notify_with_level(level, hash) end @@ -42,6 +48,20 @@ def #{const.downcase}? # def debug? def <<(message) notify_with_level(GELF::UNKNOWN, 'short_message' => message) end + + def tagged(*new_tags) + tags = formatter.current_tags + new_tags = new_tags.flatten.reject(&:blank?) + tags.concat new_tags + yield self + ensure + tags.pop(new_tags.size) + end + + def current_tags + Thread.current[:gelf_tagged_logging_tags] ||= [] + end + end # Graylog2 notifier, compatible with Ruby Logger. From 604284827faf2a1eb4dfdaeb00f6791f315ffe41 Mon Sep 17 00:00:00 2001 From: Sascha Hoellger Date: Fri, 25 May 2012 11:53:38 +0200 Subject: [PATCH 02/23] Fix wrong method call. --- lib/gelf/logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index 4a76f1a..10f303e 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -50,7 +50,7 @@ def <<(message) end def tagged(*new_tags) - tags = formatter.current_tags + tags = current_tags new_tags = new_tags.flatten.reject(&:blank?) tags.concat new_tags yield self From ccd7144aa44f1947255c74f1a0b1ae11f78cdb0d Mon Sep 17 00:00:00 2001 From: Sascha Hoellger Date: Fri, 25 May 2012 15:05:44 +0200 Subject: [PATCH 03/23] Add documentation and tests. --- lib/gelf/logger.rb | 9 +++++++++ test/test_logger.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index 10f303e..d650d85 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -68,6 +68,15 @@ def current_tags # You can use it with Rails like this: # config.logger = GELF::Logger.new("localhost", 12201, "WAN", { :facility => "appname" }) # config.colorize_logging = false + # + # Tagged logging (with tags from rack middleware) (order of tags is important) + # Adds custom gelf messages: { '_uuid_name' => , '_remote_ip_name' => } + # config.log_tags = [:uuid, :remote_ip] + # config.colorize_logging = false + # config.logger = GELF::Logger.new("localhost", 12201, 'LAN', { + # tags: [:uuid_name, :remote_ip_name], # same order as config.log_tags + # facility: 'Jobmensa 2' + # }) class Logger < Notifier include LoggerCompatibility end diff --git a/test/test_logger.rb b/test/test_logger.rb index fcd63e5..0098669 100644 --- a/test/test_logger.rb +++ b/test/test_logger.rb @@ -135,5 +135,42 @@ class TestLogger < Test::Unit::TestCase end @logger << "Message" end + + context "#tagged" do + + # logger.tagged("TAG") { logger.info "Message" } + should "support tagged method" do + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Message' && + hash['facility'] == 'gelf-rb' + end + + str = "TAG" + str.stubs(:blank?).returns(true) + + @logger.tagged(str) { @logger.info "Message" } + end + + should "set custom gelf message with tag name and tag content" do + # I want the first tag with name 'test_tag' + @logger.default_options.merge!('tags' => ['test_tag']) + + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Message' && + hash['facility'] == 'gelf-rb' && + hash['_test_tag'] == 'TAG' # TAG should be in the hash + end + + str = "TAG" + str.stubs(:blank?).returns(false) + + @logger.tagged(str) { @logger.info "Message" } + + end + + end + end end From aead6b248680e9eb72dcf4c024c4a511a76091f1 Mon Sep 17 00:00:00 2001 From: Tobias Date: Thu, 13 Sep 2012 11:51:08 +0200 Subject: [PATCH 04/23] Added formatter field to Logger --- lib/gelf/logger.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index 5415952..719a14f 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -1,6 +1,9 @@ module GELF # Methods for compatibility with Ruby Logger. module LoggerCompatibility + + attr_accessor :formatter + # Does nothing. def close end From 320dd3fc609bac140db4dddbc6215c30a7185e34 Mon Sep 17 00:00:00 2001 From: Tobias Date: Thu, 13 Sep 2012 12:04:04 +0200 Subject: [PATCH 05/23] added test for check formatter attribute --- test/test_logger.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_logger.rb b/test/test_logger.rb index fcd63e5..2125da9 100644 --- a/test/test_logger.rb +++ b/test/test_logger.rb @@ -135,5 +135,9 @@ class TestLogger < Test::Unit::TestCase end @logger << "Message" end + + should "have formatter attribute" do + @logger.formatter + end end end From e1237c030cfa98614fa485c952cc6a6fa559b160 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Sat, 26 Jan 2013 19:17:02 +0100 Subject: [PATCH 06/23] clean up rakefile --- Rakefile | 53 +++++++++-------------------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/Rakefile b/Rakefile index ffa8cd2..c53ce2e 100644 --- a/Rakefile +++ b/Rakefile @@ -51,47 +51,12 @@ rescue LoadError => e end end -begin - gem 'ruby_parser', '~> 2.0.6' - gem 'activesupport', '~> 3.0.0' - gem 'metric_fu', '~> 2.1.1' - require 'metric_fu' - - MetricFu::Configuration.run do |config| - # Saikuro is useless - config.metrics -= [:saikuro] - - config.flay = { :dirs_to_flay => ['lib'], - :minimum_score => 10 } - config.flog = { :dirs_to_flog => ['lib'] } - config.reek = { :dirs_to_reek => ['lib'] } - config.roodi = { :dirs_to_roodi => ['lib'] } - config.rcov = { :environment => 'test', - :test_files => ['test/test_*.rb'], - :rcov_opts => ["-I 'lib:test'", - "--sort coverage", - "--no-html", - "--text-coverage", - "--no-color", - "--exclude /test/,/gems/"]} - config.graph_engine = :gchart - end - -rescue LoadError, NameError => e - desc 'Generate all metrics reports' - task :'metrics:all' do - puts e.inspect - # puts e.backtrace - abort "metric_fu is not available. Run: gem install metric_fu" - end -end - -require 'rake/rdoctask' -Rake::RDocTask.new do |rdoc| - version = File.exist?('VERSION') ? File.read('VERSION') : "" - - rdoc.rdoc_dir = 'rdoc' - rdoc.title = "gelf #{version}" - rdoc.rdoc_files.include('README*') - rdoc.rdoc_files.include('lib/**/*.rb') -end +#require 'rake/rdoctask' +#Rake::RDocTask.new do |rdoc| +# version = File.exist?('VERSION') ? File.read('VERSION') : "" +# +# rdoc.rdoc_dir = 'rdoc' +# rdoc.title = "gelf #{version}" +# rdoc.rdoc_files.include('README*') +# rdoc.rdoc_files.include('lib/**/*.rb') +#end From 8c4b5bcdbcd9d3c1510fe2d46ad3d1c90101e243 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Sat, 26 Jan 2013 19:17:23 +0100 Subject: [PATCH 07/23] support pure hashes as message in logger --- VERSION | 2 +- lib/gelf/logger.rb | 15 ++++++++++++++- test/test_logger.rb | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 1892b92..88c5fb8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.2 +1.4.0 diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index 719a14f..14e8665 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -23,8 +23,20 @@ def add(level, *args) [args[0], default_options['facility']] end - hash = {'short_message' => message, 'facility' => progname} + if message.is_a?(Hash) + # Stringify keys. + hash = {} + message.each do |k,v| + hash[k.to_s] = message[k] + end + + hash['facility'] = progname + else + hash = {'short_message' => message, 'facility' => progname} + end + hash.merge!(self.class.extract_hash_from_exception(message)) if message.is_a?(Exception) + notify_with_level(level, hash) end @@ -55,4 +67,5 @@ class Logger < Notifier include LoggerCompatibility @last_chunk_id = 0 end + end diff --git a/test/test_logger.rb b/test/test_logger.rb index 2125da9..c6d2782 100644 --- a/test/test_logger.rb +++ b/test/test_logger.rb @@ -97,6 +97,43 @@ class TestLogger < Test::Unit::TestCase end @logger.add(GELF::INFO, 'Facility') { RuntimeError.new('Boom!') } end + + + ##################### + + # logger.add(Logger::INFO, { :short_message => "Some message" }) + should "implement add method with level and message from hash, facility from defaults" do + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Some message' && + hash['facility'] == 'gelf-rb' + end + @logger.add(GELF::INFO, { :short_message => "Some message" }) + end + + # logger.add(Logger::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat" }) + should "implement add method with level and message from hash, facility from defaults and some additional fields" do + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Some message' && + hash['facility'] == 'gelf-rb' && + hash['_foo'] == 'bar' && + hash['_zomg'] == 'wat' + end + @logger.add(GELF::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat"}) + end + + # logger.add(Logger::INFO, "somefac", { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat" }) + should "implement add method with level and message from hash, facility from parameters and some additional fields" do + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Some message' && + hash['facility'] == 'somefac' && + hash['_foo'] == 'bar' && + hash['_zomg'] == 'wat' + end + @logger.add(GELF::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat"}, "somefac") + end end GELF::Levels.constants.each do |const| From 215ef2f197b6113288897b81b328ffd6efac7c70 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Sat, 26 Jan 2013 19:17:35 +0100 Subject: [PATCH 08/23] Regenerate gemspec for version 1.4.0 --- gelf.gemspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gelf.gemspec b/gelf.gemspec index df6417f..e067693 100644 --- a/gelf.gemspec +++ b/gelf.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = "gelf" - s.version = "1.3.2" + s.version = "1.4.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Alexey Palazhchenko", "Lennart Koopmann"] - s.date = "2011-12-02" + s.date = "2013-01-26" 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 = [ @@ -39,7 +39,7 @@ Gem::Specification.new do |s| ] s.homepage = "http://github.com/Graylog2/gelf-rb" s.require_paths = ["lib"] - s.rubygems_version = "1.8.11" + s.rubygems_version = "1.8.24" s.summary = "Library to send GELF messages to Graylog2 logging server." if s.respond_to? :specification_version then From e8ef4fb69301fb9d7cef1f4e163fd7da655bff87 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Mon, 2 Sep 2013 20:07:39 +0400 Subject: [PATCH 09/23] Add more rubies to Travis-CI config Wow, I can commit via web interface on iPad from a train! --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0f92af8..7f8998c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,11 @@ rvm: - ree - 1.9.2 - 1.9.3 - - jruby + - 2.0.0 + - jruby-18mode + - jruby-19mode + - rbx-18mode + - rbx-19mode notifications: irc: "irc.freenode.org#graylog2" From 460977e64be4b8d7d3b818dc2e2bd9acdb405a4f Mon Sep 17 00:00:00 2001 From: John Richardson Date: Wed, 4 Sep 2013 13:21:11 -0500 Subject: [PATCH 10/23] fixes chunking for subclasses of GELF::Notifier --- lib/gelf/logger.rb | 1 - lib/gelf/notifier.rb | 2 +- test/test_notifier.rb | 27 +++++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index 14e8665..e730862 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -65,7 +65,6 @@ def <<(message) # config.colorize_logging = false class Logger < Notifier include LoggerCompatibility - @last_chunk_id = 0 end end diff --git a/lib/gelf/notifier.rb b/lib/gelf/notifier.rb index 6f4670d..46d9d6e 100644 --- a/lib/gelf/notifier.rb +++ b/lib/gelf/notifier.rb @@ -208,7 +208,7 @@ def datagrams_from_hash # 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 + id = GELF::Notifier.last_chunk_id += 1 msg_id = Digest::MD5.digest("#{Time.now.to_f}-#{id}")[0, 8] num, count = 0, (data.count.to_f / @max_chunk_size).ceil data.each_slice(@max_chunk_size) do |slice| diff --git a/test/test_notifier.rb b/test/test_notifier.rb index 01b253d..986bd9b 100644 --- a/test/test_notifier.rb +++ b/test/test_notifier.rb @@ -155,10 +155,33 @@ class TestNotifier < Test::Unit::TestCase should "split long data" do srand(1) # for stable tests hash = { 'version' => '1.0', 'short_message' => 'message' } - hash.merge!('something' => (0..3000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good + hash.merge!('something' => (0..30000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good @notifier.instance_variable_set('@hash', hash) datagrams = @notifier.__send__(:datagrams_from_hash) - assert_equal 2, datagrams.count + assert -> {datagrams.count > 1}, "There should be more than one datagram" + datagrams.each_index do |i| + datagram = datagrams[i] + assert_instance_of String, datagram + assert datagram[0..1] == "\x1e\x0f" # chunked GELF magic number + # datagram[2..9] is a message id + assert_equal i, datagram[10].ord + assert_equal datagrams.count, datagram[11].ord + end + end + + should "split long data when subclassed" do + class MyNotifier < GELF::Notifier; end + + @notifier = MyNotifier.new('host', 1234) + @sender = mock + @notifier.instance_variable_set('@sender', @sender) + + srand(1) # for stable tests + hash = { 'version' => '1.0', 'short_message' => 'message' } + hash.merge!('something' => (0..30000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good + @notifier.instance_variable_set('@hash', hash) + datagrams = @notifier.__send__(:datagrams_from_hash) + assert -> {datagrams.count > 1}, "There should be more than one datagram" datagrams.each_index do |i| datagram = datagrams[i] assert_instance_of String, datagram From 19d1d87a4a4d49345d013d677da7a38a3c7b65c3 Mon Sep 17 00:00:00 2001 From: John Richardson Date: Wed, 4 Sep 2013 13:30:43 -0500 Subject: [PATCH 11/23] uses smaller data for chunking test --- test/test_notifier.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_notifier.rb b/test/test_notifier.rb index 986bd9b..f70a160 100644 --- a/test/test_notifier.rb +++ b/test/test_notifier.rb @@ -155,10 +155,10 @@ class TestNotifier < Test::Unit::TestCase should "split long data" do srand(1) # for stable tests hash = { 'version' => '1.0', 'short_message' => 'message' } - hash.merge!('something' => (0..30000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good + hash.merge!('something' => (0..3000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good @notifier.instance_variable_set('@hash', hash) datagrams = @notifier.__send__(:datagrams_from_hash) - assert -> {datagrams.count > 1}, "There should be more than one datagram" + assert_equal 2, datagrams.count datagrams.each_index do |i| datagram = datagrams[i] assert_instance_of String, datagram @@ -178,10 +178,10 @@ class MyNotifier < GELF::Notifier; end srand(1) # for stable tests hash = { 'version' => '1.0', 'short_message' => 'message' } - hash.merge!('something' => (0..30000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good + hash.merge!('something' => (0..3000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good @notifier.instance_variable_set('@hash', hash) datagrams = @notifier.__send__(:datagrams_from_hash) - assert -> {datagrams.count > 1}, "There should be more than one datagram" + assert_equal 2, datagrams.count datagrams.each_index do |i| datagram = datagrams[i] assert_instance_of String, datagram From 69cdd1ed51715bd08cc4fc1e9c66098e324d180f Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Wed, 4 Sep 2013 22:28:03 +0200 Subject: [PATCH 12/23] lmao if you had no gemfile in 2013 --- Gemfile | 5 ++++ Gemfile.lock | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Rakefile | 1 - 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..bf0ba95 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem "shoulda", "~> 3.5.0" +gem "jeweler", "~> 1.8.7" +gem "mocha", "~> 0.14.0" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..95544fc --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,74 @@ +GEM + remote: https://rubygems.org/ + specs: + activesupport (4.0.0) + i18n (~> 0.6, >= 0.6.4) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + addressable (2.3.5) + atomic (1.1.13) + builder (3.2.2) + faraday (0.8.8) + multipart-post (~> 1.2.0) + git (1.2.6) + github_api (0.10.1) + addressable + faraday (~> 0.8.1) + hashie (>= 1.2) + multi_json (~> 1.4) + nokogiri (~> 1.5.2) + oauth2 + hashie (2.0.5) + highline (1.6.19) + httpauth (0.2.0) + i18n (0.6.5) + jeweler (1.8.7) + builder + bundler (~> 1.0) + git (>= 1.2.5) + github_api (= 0.10.1) + highline (>= 1.6.15) + nokogiri (= 1.5.10) + rake + rdoc + json (1.8.0) + jwt (0.1.8) + multi_json (>= 1.5) + metaclass (0.0.1) + minitest (4.7.5) + mocha (0.14.0) + metaclass (~> 0.0.1) + multi_json (1.7.9) + multi_xml (0.5.5) + multipart-post (1.2.0) + nokogiri (1.5.10) + oauth2 (0.9.2) + faraday (~> 0.8) + httpauth (~> 0.2) + jwt (~> 0.1.4) + multi_json (~> 1.0) + multi_xml (~> 0.5) + rack (~> 1.2) + rack (1.5.2) + rake (10.1.0) + rdoc (4.0.1) + json (~> 1.4) + shoulda (3.5.0) + shoulda-context (~> 1.0, >= 1.0.1) + shoulda-matchers (>= 1.4.1, < 3.0) + shoulda-context (1.1.5) + shoulda-matchers (2.3.0) + activesupport (>= 3.0.0) + thread_safe (0.1.2) + atomic + tzinfo (0.3.37) + +PLATFORMS + ruby + +DEPENDENCIES + jeweler (~> 1.8.7) + mocha (~> 0.14.0) + shoulda (~> 3.5.0) diff --git a/Rakefile b/Rakefile index c53ce2e..24ad67e 100644 --- a/Rakefile +++ b/Rakefile @@ -33,7 +33,6 @@ Rake::TestTask.new(:test) do |test| test.verbose = true end -task :test => :check_dependencies task :default => :test begin From 55aedaa5219429100977de16b0867018aa16c461 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Wed, 4 Sep 2013 22:59:26 +0200 Subject: [PATCH 13/23] downgrade shoulda --- Gemfile | 2 +- Gemfile.lock | 21 ++------------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index bf0ba95..c7bce43 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source "https://rubygems.org" -gem "shoulda", "~> 3.5.0" +gem "shoulda", "~> 2.11.3" gem "jeweler", "~> 1.8.7" gem "mocha", "~> 0.14.0" diff --git a/Gemfile.lock b/Gemfile.lock index 95544fc..7f4fb36 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,7 @@ GEM remote: https://rubygems.org/ specs: - activesupport (4.0.0) - i18n (~> 0.6, >= 0.6.4) - minitest (~> 4.2) - multi_json (~> 1.3) - thread_safe (~> 0.1) - tzinfo (~> 0.3.37) addressable (2.3.5) - atomic (1.1.13) builder (3.2.2) faraday (0.8.8) multipart-post (~> 1.2.0) @@ -23,7 +16,6 @@ GEM hashie (2.0.5) highline (1.6.19) httpauth (0.2.0) - i18n (0.6.5) jeweler (1.8.7) builder bundler (~> 1.0) @@ -37,7 +29,6 @@ GEM jwt (0.1.8) multi_json (>= 1.5) metaclass (0.0.1) - minitest (4.7.5) mocha (0.14.0) metaclass (~> 0.0.1) multi_json (1.7.9) @@ -55,15 +46,7 @@ GEM rake (10.1.0) rdoc (4.0.1) json (~> 1.4) - shoulda (3.5.0) - shoulda-context (~> 1.0, >= 1.0.1) - shoulda-matchers (>= 1.4.1, < 3.0) - shoulda-context (1.1.5) - shoulda-matchers (2.3.0) - activesupport (>= 3.0.0) - thread_safe (0.1.2) - atomic - tzinfo (0.3.37) + shoulda (2.11.3) PLATFORMS ruby @@ -71,4 +54,4 @@ PLATFORMS DEPENDENCIES jeweler (~> 1.8.7) mocha (~> 0.14.0) - shoulda (~> 3.5.0) + shoulda (~> 2.11.3) From 38e85a6cd42c022c9dd46a8594fc474f0ffd8c0c Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Thu, 5 Sep 2013 12:19:43 +0200 Subject: [PATCH 14/23] fix #rubby 2.0 tests thx for the hint, @edmundoa :) fixes #13 --- test/test_notifier.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/test_notifier.rb b/test/test_notifier.rb index f70a160..fc178ad 100644 --- a/test/test_notifier.rb +++ b/test/test_notifier.rb @@ -149,7 +149,15 @@ class TestNotifier < Test::Unit::TestCase datagrams = @notifier.__send__(:datagrams_from_hash) assert_equal 1, datagrams.count assert_instance_of String, datagrams[0] - assert_equal "\x78\x9c", datagrams[0][0..1] # zlib header + + asserted = "\x78\x9c" + if RUBY_VERSION[0].to_i >= 2 + # lol well yeah, Rubby. + # http://stackoverflow.com/questions/15843684/binary-string-literals-in-ruby-2-0 + asserted = asserted.b + end + + assert_equal asserted, datagrams[0][0..1] # zlib header end should "split long data" do From 5f94664478c622fd9bcedb774981bfb343ace309 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Thu, 5 Sep 2013 12:36:44 +0200 Subject: [PATCH 15/23] debugging for travis-ci --- test/test_notifier.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_notifier.rb b/test/test_notifier.rb index fc178ad..60d966a 100644 --- a/test/test_notifier.rb +++ b/test/test_notifier.rb @@ -152,6 +152,7 @@ class TestNotifier < Test::Unit::TestCase asserted = "\x78\x9c" if RUBY_VERSION[0].to_i >= 2 + puts "I'm a Ruby > 2.0.0. Enforcing ASCII-8BIT. (#{RUBY_VERSION}/#{RUBY_VERSION[0].to_i})" # lol well yeah, Rubby. # http://stackoverflow.com/questions/15843684/binary-string-literals-in-ruby-2-0 asserted = asserted.b From 69a0372fad681cda8bc1a038ef74169ee7ba4421 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Thu, 5 Sep 2013 12:45:07 +0200 Subject: [PATCH 16/23] ruby 1.8.7 compat #13 --- test/test_notifier.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_notifier.rb b/test/test_notifier.rb index 60d966a..ada327f 100644 --- a/test/test_notifier.rb +++ b/test/test_notifier.rb @@ -151,8 +151,8 @@ class TestNotifier < Test::Unit::TestCase assert_instance_of String, datagrams[0] asserted = "\x78\x9c" - if RUBY_VERSION[0].to_i >= 2 - puts "I'm a Ruby > 2.0.0. Enforcing ASCII-8BIT. (#{RUBY_VERSION}/#{RUBY_VERSION[0].to_i})" + if RUBY_VERSION[0,1].to_i >= 2 + puts "I'm a Ruby > 2.0.0. Enforcing ASCII-8BIT. (#{RUBY_VERSION}/#{RUBY_VERSION[0,1].to_i})" # lol well yeah, Rubby. # http://stackoverflow.com/questions/15843684/binary-string-literals-in-ruby-2-0 asserted = asserted.b From 467ce5fdd2f850649a8350b4b3d6ff3d67c416ae Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Thu, 5 Sep 2013 13:12:52 +0200 Subject: [PATCH 17/23] added travis-ci build status, updated copyright years and supported ruby versions --- README.rdoc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.rdoc b/README.rdoc index 904a92d..9d7be50 100644 --- a/README.rdoc +++ b/README.rdoc @@ -2,7 +2,9 @@ This is the new GELF gem written by Alexey Palazhchenko. It is based on the old gem by Lennart Koopmann and allows you to send GELF messages to Graylog2 server instances. See http://www.graylog2.org/about/gelf for more information about GELF and http://rdoc.info/github/Graylog2/gelf-rb/master/frames for API documentation. -Works with Ruby 1.8.7 and 1.9.x. 1.8.6 is not supported. +Works with Ruby 1.8.7, 1.9.x. and 2.0.x. + +{Build Status}[https://travis-ci.org/Graylog2/gelf-rb] == Note on Patches/Pull Requests @@ -15,4 +17,4 @@ Works with Ruby 1.8.7 and 1.9.x. 1.8.6 is not supported. == Copyright -Copyright (c) 2010-2011 Lennart Koopmann and Alexey Palazhchenko. See LICENSE for details. +Copyright (c) 2010-2013 Lennart Koopmann and Alexey Palazhchenko. See LICENSE for details. From ef2d28ef4135eb7018b54d47ce52050ffbc1b392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awosz?= Date: Tue, 8 Oct 2013 11:19:18 +0200 Subject: [PATCH 18/23] use default facility if progname is blank --- lib/gelf/logger.rb | 2 ++ test/test_logger.rb | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index e730862..63e416e 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -35,6 +35,8 @@ def add(level, *args) hash = {'short_message' => message, 'facility' => progname} end + hash['facility'] = default_options['facility'] unless progname + hash.merge!(self.class.extract_hash_from_exception(message)) if message.is_a?(Exception) notify_with_level(level, hash) diff --git a/test/test_logger.rb b/test/test_logger.rb index c6d2782..668b90a 100644 --- a/test/test_logger.rb +++ b/test/test_logger.rb @@ -66,6 +66,27 @@ class TestLogger < Test::Unit::TestCase @logger.add(GELF::INFO, 'Message', 'Facility') end + # logger.add(Logger::INFO, 'Message', 'Facility') + should "use facility from initialization if facility is nil" do + logger = GELF::Logger.new('localhost', 12202, 'WAN', facility: 'foo-bar') + logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Message' && + hash['facility'] == 'foo-bar' + end + logger.add(GELF::INFO, 'Message', nil) + end + + # logger.add(Logger::INFO, 'Message', 'Facility') + should "use default facility if facility is nil" do + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::INFO && + hash['short_message'] == 'Message' && + hash['facility'] == 'gelf-rb' + end + @logger.add(GELF::INFO, 'Message', nil) + end + # logger.add(Logger::INFO, RuntimeError.new('Boom!'), 'Facility') should "implement add method with level, exception and facility from parameters" do @logger.expects(:notify_with_level!).with do |level, hash| From 429ac9b5b2c907c96be928c5bb22ed508e1924cd Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Wed, 9 Oct 2013 18:29:25 +0200 Subject: [PATCH 19/23] fix breaking test. ruby 1.8.7 compat for #16 --- test/test_logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_logger.rb b/test/test_logger.rb index 668b90a..0b3a137 100644 --- a/test/test_logger.rb +++ b/test/test_logger.rb @@ -68,7 +68,7 @@ class TestLogger < Test::Unit::TestCase # logger.add(Logger::INFO, 'Message', 'Facility') should "use facility from initialization if facility is nil" do - logger = GELF::Logger.new('localhost', 12202, 'WAN', facility: 'foo-bar') + logger = GELF::Logger.new('localhost', 12202, 'WAN', :facility => 'foo-bar') logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'Message' && From 2f5d3c33f1ca3f8e7c1d1fa915ff0a903a6cf31b Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Tue, 15 Apr 2014 16:21:00 +0200 Subject: [PATCH 20/23] contributing guidelines --- CONTRIBUTING.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..9c7acec --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,34 @@ +Thank you very much for considering contributing to the Graylog2 project! We <3 our community. + +To make handling all the community contributions as easy as possible for us we ask you to follow these steps as good as possible: + +## Bug reports + + 1. Please sign the [TORCH Contributor Agreement](https://github.com/Graylog2/graylog2-web-interface/raw/0.20/ContributorAgreement.pdf) if your issue contains + any code. We cannot go on without this. + 2. Search the issues of both the [graylog2-server](https://github.com/Graylog2/graylog2-server) and [graylog2-web-interface](https://github.com/Graylog2/graylog2-web-interface) + repositories for opened or closed issues to avoid duplicating effort. + 3. Be clear about the issue. It is always better to include too much information than too little. Include screenshots explaining the problem if you can. + 4. Always include the versions of affected Graylog2 components you are running. + 5. Provide exact steps to reproduce the issue if at any possible. + +## Pull requests + + 1. Please sign the [TORCH Contributor Agreement](https://github.com/Graylog2/graylog2-web-interface/raw/0.20/ContributorAgreement.pdf) before sending pull requests. + We cannot accept code without this. + 2. Search the issues of both the [graylog2-server](https://github.com/Graylog2/graylog2-server) and [graylog2-web-interface](https://github.com/Graylog2/graylog2-web-interface) + repositories for opened or closed issues to avoid duplicating effort. + 3. Contact the [TORCH](http://www.torch.sh/) team via [any communication channel](http://graylog2.org/support) (contact form, mailing list, IRC, Github issue tracker) before working on a big change to make sure + that there are chances of acceptance. + 4. Include tests if at any possible. + 5. Submit the pull request. + +## Feature requests + +We are always happy about any feature request! Do not hesitate to create as many feature request issues as you want. + + 1. Please sign the [TORCH Contributor Agreement](https://github.com/Graylog2/graylog2-web-interface/raw/0.20/ContributorAgreement.pdf) if your issue contains + any code. We cannot go on without this. + 2. Search the issues of both the [graylog2-server](https://github.com/Graylog2/graylog2-server) and [graylog2-web-interface](https://github.com/Graylog2/graylog2-web-interface) + repositories for opened or closed issues to avoid duplicating effort. + 3. Explain exactly what feature you'd like to see and always include a use case for why you think it is useful. From 25d4d3e65c80fa7630274e8f4c5558580006d9e0 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Tue, 15 Apr 2014 17:14:27 +0200 Subject: [PATCH 21/23] Add basic API usage information to README This adds some basic API information to the README file, which didn't have any documentation about the API before. Some parts are copied from the wiki. --- README.rdoc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.rdoc b/README.rdoc index 9d7be50..a3fd69c 100644 --- a/README.rdoc +++ b/README.rdoc @@ -6,6 +6,39 @@ Works with Ruby 1.8.7, 1.9.x. and 2.0.x. {Build Status}[https://travis-ci.org/Graylog2/gelf-rb] +== Usage +=== Gelf::Notifier + +This allows you to sent arbitary messages via UDP to your Graylog2 server. + + n = GELF::Notifier.new("localhost", 12201) + + # Send with custom attributes and an additional parameter "foo" + n.notify!(:short_message => "foo", :full_message => "something here\n\nbacktrace?!", :_foo => "bar") + + # Pass any object that responds to .to_hash + n.notify!(Exception.new) + +=== Gelf::Logger + +The Gelf::Logger is compatible with the standard Ruby Logger interface and can be used interchangeably. +Under the hood it uses Gelf::Notifier to send log messages via UDP to Graylog2. + + logger = GELF::Logger.new("localhost", 12201, "WAN", { :facility => "appname" }) + + logger.debug "foobar" + logger.info "foobar" + logger.warn "foobar" + logger.error "foobar" + logger.fatal "foobar" + + logger << "foobar" + +Since it's compatible with the Logger interface, you can also use it in your Rails application: + + # config/environments/production.rb + config.logger = GELF::Logger.new("localhost", 12201, "WAN", { :facility => "appname" }) + == Note on Patches/Pull Requests * Fork the project. From e93e3508e86a7282c67623e39bcadb6dcc472536 Mon Sep 17 00:00:00 2001 From: Lennart Koopmann Date: Tue, 15 Apr 2014 19:35:46 +0200 Subject: [PATCH 22/23] don't run on all those rubbies --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7f8998c..3e349e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,7 @@ rvm: - 1.9.2 - 1.9.3 - 2.0.0 - - jruby-18mode - jruby-19mode - - rbx-18mode - - rbx-19mode notifications: irc: "irc.freenode.org#graylog2" From a594b611dd56ed045e89090a0f73dff6726f51fa Mon Sep 17 00:00:00 2001 From: Ryan LeFevre Date: Mon, 21 Apr 2014 14:48:10 -0400 Subject: [PATCH 23/23] Add formatter support --- lib/gelf/logger.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/gelf/logger.rb b/lib/gelf/logger.rb index 2b93e3d..3e1208b 100644 --- a/lib/gelf/logger.rb +++ b/lib/gelf/logger.rb @@ -46,7 +46,7 @@ def add(level, *args) end end - notify_with_level(level, hash) + notify_with_level(level, format_message(level, Time.now, progname, hash)) end # Redefines methods in +Notifier+. @@ -80,6 +80,11 @@ def current_tags Thread.current[:gelf_tagged_logging_tags] ||= [] end + def format_message(severity, datetime, progname, message) + return message if formatter.nil? + formatter.call(severity, datetime, progname, message) + end + end # Graylog2 notifier, compatible with Ruby Logger.