Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Split on new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Aug 22, 2016
1 parent 8e264ab commit 847a088
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/timber/log_devices/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ def final_message
end

def log_line
@log_line ||= LogLine.new(message.chomp)
@log_line ||= LogLine.new(message)
end

def encoded_context
@encoded_context ||= log_line.context_snapshot.to_logfmt
end
end

NEWLINE = "\n".freeze

attr_accessor :colorize

def initialize(io = STDOUT, options = {})
Expand All @@ -58,7 +60,9 @@ def close(*args)
end

def write(message)
line_class.new(io, message, colorize).write
message.chomp.split(NEWLINE).each do |message|
line_class.new(io, message, colorize).write
end
rescue Exception => e
Config.logger.exception(e)
raise e
Expand Down
15 changes: 13 additions & 2 deletions spec/timber/log_devices/heroku_logplex_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "spec_helper"

describe Timber::LogDevices::HerokuLogplex do
let(:io) { STDOUT }
let(:log_device) { described_class.new }

describe ".write" do
Expand All @@ -12,7 +13,7 @@
end

it "writes a proper logfmt line" do
expect(STDOUT).to receive(:write).with("this is a message \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=0 _version=1 _hierarchy=[server]\e[0m\n")
expect(io).to receive(:write).with("this is a message \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=0 _version=1 _hierarchy=[server]\e[0m\n")
# Notice we do not have dt for the log line since Heroku provides this
log_device.write("this is a message\n")
end
Expand All @@ -25,10 +26,20 @@

# No need for the heroku context since logplex includes that data by default
it "does not include the heroku context" do
expect(STDOUT).to receive(:write).with("this is a message \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=0 _version=1 _hierarchy=[server,server.heroku]\e[0m\n")
expect(io).to receive(:write).with("this is a message \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=0 _version=1 _hierarchy=[server,server.heroku]\e[0m\n")
# Notice we do not have dt for the log line since Heroku provides this
log_device.write("this is a message\n")
end
end

context "with multiple lines" do |variable|
it "does not include the heroku context" do
expect(io).to receive(:write).with("line 1 \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=0 _version=1 _hierarchy=[server]\e[0m\n")
expect(io).to receive(:write).with("line 2 \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=1 _version=1 _hierarchy=[server]\e[0m\n")
expect(io).to receive(:write).with("line 3 \e[30m[timber.io] server.hostname=computer-name.domain.com server._dt=2016-09-01T12:00:00.000000Z server._version=1 server._index=2 _version=1 _hierarchy=[server]\e[0m\n")
# Notice we do not have dt for the log line since Heroku provides this
log_device.write("line 1\nline 2\nline 3\n")
end
end
end
end

0 comments on commit 847a088

Please sign in to comment.