diff --git a/lib/rackstash/message.rb b/lib/rackstash/message.rb index 786e218..b300a4d 100644 --- a/lib/rackstash/message.rb +++ b/lib/rackstash/message.rb @@ -42,11 +42,6 @@ module Rackstash @time = dup_freeze(time) @progname = dup_freeze(progname) @formatter = formatter - - # Freeze the newly created message to ensure it can't be changed. - # All passed values are also effectively frozen, making the Message an - # immutable object. - freeze end def severity_label @@ -56,8 +51,14 @@ module Rackstash def to_s cleanup @formatter.call(severity_label, @time, @progname, @message) end - alias_method :to_str, :to_s - alias_method :as_json, :to_s + + alias :as_json :to_s + # Messages are implicitly conversible to Strings + alias :to_str :to_s + + def to_json + as_json.to_json + end private diff --git a/spec/rackstash/message_spec.rb b/spec/rackstash/message_spec.rb index 29f3f8a..2932247 100644 --- a/spec/rackstash/message_spec.rb +++ b/spec/rackstash/message_spec.rb @@ -180,11 +180,24 @@ describe Rackstash::Message do expect(message.to_s).to eql '#' end - end + it 'is aliased to to_str' do + message = Rackstash::Message.new('hello world') + expect(message.to_str).to eql 'hello world' + end - describe '#frozen?' do - it 'is always true' do - expect(Rackstash::Message.new('Beep boop')).to be_frozen + it 'is aliased to as_json' do + message = Rackstash::Message.new('hello world') + expect(message.as_json).to eql 'hello world' end end + + it 'formats the message as JSON' do + message = Rackstash::Message.new('hello world') + as_json = 'hello world' + + expect(message).to receive(:as_json).and_return(as_json) + expect(as_json).to receive(:to_json).and_return('"json string"') + + expect(message.to_json).to eql '"json string"' + end end