Skip to content

Commit

Permalink
support pure hashes as message in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Koopmann committed Jan 26, 2013
1 parent e1237c0 commit 8c4b5bc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.2
1.4.0
15 changes: 14 additions & 1 deletion lib/gelf/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -55,4 +67,5 @@ class Logger < Notifier
include LoggerCompatibility
@last_chunk_id = 0
end

end
37 changes: 37 additions & 0 deletions test/test_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 8c4b5bc

Please sign in to comment.