Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringify key :message in the base formatter #116

Merged
merged 1 commit into from
May 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/logstash-logger/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def build_event(message, severity, time)
when LogStash::Event
data.clone
when Hash
event_data = data.merge("@timestamp".freeze => time)
event_data = data.clone
event_data['message'.freeze] = event_data.delete(:message) if event_data.key?(:message)
Copy link

@stereobooster stereobooster Feb 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constanize strings as well e.g.

MESSAGE_FIELD = 'message'.freeze
...
event_data[MESSAGE_FIELD]

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recollection is that Ruby will optimize this sort of code anyway, and therefore extracting to a constant isn't necessary. Can't find a source for this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I missed this optimisation somehow. Here is the proof (at least for ruby 2.2.6p396 (2016-11-15 revision 56800)):

irb(main):001:0> "asd".object_id
=> 70178538223500
irb(main):002:0> "asd".object_id
=> 70178538189060
irb(main):003:0> "asd".freeze.object_id
=> 70178538223620
irb(main):004:0> "asd".freeze.object_id
=> 70178538223620

Copy link

@stereobooster stereobooster Feb 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but for ruby 2.0.0p247 (2013-06-27 revision 41674)

irb(main):001:0> "asd".object_id
=> 70313257930780
irb(main):002:0> "asd".object_id
=> 70313241344880
irb(main):003:0> "asd".freeze.object_id
=> 70313241338520
irb(main):004:0> "asd".freeze.object_id
=> 70313241348500
irb(main):005:0> "asd".freeze.object_id

And taking into account that library claims to support ruby 2.0 constanization still makes sense

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the legwork. And good point, LogStashLogger support goes back to 1.9, so it should definitely do whatever optimizations are necessary.

event_data['@timestamp'.freeze] = time
LogStash::Event.new(event_data)
else
LogStash::Event.new("message".freeze => msg2str(data), "@timestamp".freeze => time)
Expand All @@ -37,7 +39,7 @@ def build_event(message, severity, time)
event['host'.freeze] ||= HOST

current_tags.each { |tag| event.tag(tag) }

LogStashLogger.configuration.customize_event_block.call(event) if LogStashLogger.configuration.customize_event_block.respond_to?(:call)

# In case Time#to_json has been overridden
Expand All @@ -48,7 +50,7 @@ def build_event(message, severity, time)
if LogStashLogger.configuration.max_message_size
event['message'.freeze] = event['message'.freeze].byteslice(0, LogStashLogger.configuration.max_message_size)
end

event
end
end
Expand Down