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

Commit

Permalink
Disallow recursion when writing log lines
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Sep 6, 2016
1 parent ea91e19 commit e97eae1
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 31 deletions.
3 changes: 2 additions & 1 deletion lib/timber/contexts/organizations/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def method_name
end

def organization
controller.respond_to?(method_name, true) ? controller.send(method_name) : nil
return @organization if defined?(@organization)
@organization = controller.respond_to?(method_name, true) ? controller.send(method_name) : nil
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/timber/contexts/users/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def method_name
end

def user
controller.respond_to?(method_name, true) ? controller.send(method_name) : nil
return @user if defined?(@user)
@user = controller.respond_to?(method_name, true) ? controller.send(method_name) : nil
end
end
end
Expand Down
38 changes: 37 additions & 1 deletion lib/timber/log_device.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
module Timber
class LogDevice
end
NEWLINE = "\n".freeze
SPLIT_LINES = true

def write(message)
return false if ignoring?
ignore do
messages(message).each do |message_part|
log_line = LogLine.new(message_part)
write_log_line(log_line)
end
end
true
rescue Exception => e
Config.logger.exception(e)
raise e
end

private
def ignore(&block)
@ignoring = true
yield
ensure
@ignoring = false
end

def ignoring?
@ignoring == true
end

def messages(message)
message.chomp.split(NEWLINE)
end

def write_formatted(formatted_message)
raise NotImplementedError.new
end
end
end
5 changes: 0 additions & 5 deletions lib/timber/log_devices/heroku_logplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ def initialize(_options = {})
formatter.extend HybridFormatter
end
end

private
def line_class
Line
end
end
end
end
14 changes: 6 additions & 8 deletions lib/timber/log_devices/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module Timber
module LogDevices
class HTTP < LogDevice
SPLIT_LINES = false

attr_reader :application_key

def initialize(application_key = nil)
Expand All @@ -17,14 +19,10 @@ def initialize(application_key = nil)
def close(*args)
end

def write(message)
log_line = LogLine.new(message.chmop)
LogPile.get(application_key).drop(log_line)
message
rescue Exception => e
Config.logger.exception(e)
raise e
end
private
def write_log_line(log_line)
LogPile.get(application_key).drop(log_line)
end
end
end
end
17 changes: 4 additions & 13 deletions lib/timber/log_devices/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module LogDevices
# The IO log device works with any IO object. That is, any object that
# response to #write(message).
class IO < LogDevice
NEWLINE = "\n".freeze

attr_reader :formatter

# Instantiates a new Timber IO log device.
Expand All @@ -29,19 +27,12 @@ def close(*_args)
io.close
end

def write(message)
message.chomp.split(NEWLINE).each do |message|
log_line = LogLine.new(message)
message = formatter.format(log_line)
io.write(message + "\n")
private
def write_log_line(log_line)
formatted_message = formatter.format(log_line)
io.write(formatted_message + "\n")
end
true
rescue Exception => e
Config.logger.exception(e)
raise e
end

private
def io
@io
end
Expand Down
1 change: 0 additions & 1 deletion lib/timber/log_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class InvalidMessageError < ArgumentError; end
attr_reader :context_snapshot, :dt, :line_indexes, :message

def initialize(message)
puts "\n\n#{message}\n#{caller.join("\n")}"
@dt = Time.now.utc # Capture the time as soon as possible
message = message.to_s
if message.bytesize > APISettings::MESSAGE_BYTE_SIZE_MAX
Expand Down
15 changes: 14 additions & 1 deletion spec/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@
ActiveRecord::Schema.define do
self.verbose = false

create_table :organizations, :force => true do |t|
t.string :name

t.timestamps :null => false
end

create_table :users, :force => true do |t|
t.string :first_name
t.integer :age
t.string :email
t.string :first_name

t.timestamps :null => false
end

end

class Organization < ::ActiveRecord::Base
end

class User < ::ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions spec/timber/probes/action_dispatch_debug_exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def method_for_action(action_name)
Object.send(:remove_const, :ExceptionController)
end

let(:logger_context_class) { Timber::Contexts::Logger }
let(:rack_request_context_class) { Timber::Contexts::HTTPRequests::Rack }
let(:request_context_class) { Timber::Contexts::HTTPRequests::ActionControllerSpecific }
let(:organization_context_class) { Timber::Contexts::Organizations::ActionController }
Expand All @@ -33,6 +34,7 @@ def method_for_action(action_name)
let(:exception_context_class) { Timber::Contexts::Exception }

it "should set the context" do
expect(Timber::CurrentContext).to receive(:add).with(kind_of(logger_context_class)).and_yield.once
expect(Timber::CurrentContext).to receive(:add).with(kind_of(rack_request_context_class)).and_yield.once
expect(Timber::CurrentContext).to receive(:add).with(kind_of(request_context_class), kind_of(organization_context_class), kind_of(user_context_class), kind_of(response_context_class)).and_yield.once
expect(Timber::CurrentContext).to receive(:add).with(kind_of(exception_context_class)).and_yield
Expand Down

0 comments on commit e97eae1

Please sign in to comment.