Skip to content

Commit

Permalink
Merge pull request #58 from tilfin/feature/data_as_hash
Browse files Browse the repository at this point in the history
Fix data not a Hash
  • Loading branch information
tilfin authored Mar 8, 2018
2 parents 472677c + bf82c04 commit fea61ac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
56 changes: 32 additions & 24 deletions lib/ougai/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,46 +102,54 @@ def to_item(args)
end
end

def create_item_with_1arg(msg)
def create_item_with_1arg(arg)
item = {}
if msg.is_a?(Exception)
item[:msg] = msg.to_s
set_exc(item, msg)
elsif msg.is_a?(Hash)
item[:msg] = @default_message unless msg.key?(:msg)
item.merge!(msg)
if arg.is_a?(Exception)
item[:msg] = arg.to_s
set_exc(item, arg)
elsif arg.is_a?(String)
item[:msg] = arg
else
item[:msg] = msg.to_s
item.merge!(as_hash(arg))
item[:msg] ||= @default_message
end
item
end

def create_item_with_2args(msg, ex)
def create_item_with_2args(arg1, arg2)
item = {}
if ex.is_a?(Exception)
item[:msg] = msg.to_s
set_exc(item, ex)
elsif ex.is_a?(Hash)
item.merge!(ex)
if msg.is_a?(Exception)
set_exc(item, msg)
else
item[:msg] = msg.to_s
end
if arg2.is_a?(Exception) # msg, ex
item[:msg] = arg1.to_s
set_exc(item, arg2)
elsif arg1.is_a?(Exception) # ex, data
set_exc(item, arg1)
item.merge!(as_hash(arg2))
item[:msg] ||= arg1.to_s
else # msg, data
item[:msg] = arg1.to_s
item.merge!(as_hash(arg2))
end
item
end

def create_item_with_3args(msg, ex, data)
item = {}
set_exc(item, ex) if ex.is_a?(Exception)
item.merge!(data) if data.is_a?(Hash)
item[:msg] = msg.to_s
item
{}.tap do |item|
set_exc(item, ex) if ex.is_a?(Exception)
item.merge!(as_hash(data))
item[:msg] = msg.to_s
end
end

def set_exc(item, exc)
item[@exc_key] = @formatter.serialize_exc(exc)
end

def as_hash(data)
if data.is_a?(Hash) || data.respond_to?(:to_hash)
data
else
{ data: data }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ougai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ougai
VERSION = '1.6.4'
VERSION = '1.6.5'
end
28 changes: 28 additions & 0 deletions spec/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@
end
end

context 'with data that can respond to_hash' do
it 'outputs valid' do
class Dummy
def to_hash
{ foo: 1 }
end
end

logger.send(method, Dummy.new)
expect(item).to be_log_message('No message', log_level)
expect(item).to include_data(foo: 1)
end
end

context 'with data that cannot respond to_hash' do
it '(array) outputs valid' do
logger.send(method, ['bar', 2])
expect(item).to be_log_message('No message', log_level)
expect(item).to include_data(data: ['bar', 2])
end

it '(number) outputs valid' do
logger.send(method, 999)
expect(item).to be_log_message('No message', log_level)
expect(item).to include_data(data: 999)
end
end

context 'with message and data' do
it 'outputs valid' do
logger.send(method, log_msg, data_id: 99, action: 'insert')
Expand Down

0 comments on commit fea61ac

Please sign in to comment.