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

Do not fail in Loga::Event#to_s when there is no timestamp #88

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion lib/loga/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(opts = {})
end

def to_s
output = ["#{timestamp.iso8601(3)} #{message}"]
output = [message_with_time]
if exception
output.push exception.to_s
output.push exception.backtrace.join("\n")
Expand All @@ -23,6 +23,14 @@ def to_s

private

def message_with_time
if timestamp
"#{timestamp.iso8601(3)} #{message}"
else
message
end
end

# Guard against Encoding::UndefinedConversionError
# http://stackoverflow.com/questions/13003287/encodingundefinedconversionerror
def safe_encode(text)
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/loga/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
expect(subject.to_s).to eql("#{time_anchor.iso8601(3)} Hello World")
end
end

context 'when no timestamp' do
let(:opts) { { message: 'Hello World' } }

it 'will render the message without it' do
expect(subject.to_s).to eql('Hello World')
end
end
end

describe '#inspect' do
Expand Down