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|