Skip to content

Commit

Permalink
Merge pull request huginn#734 from cantino/object_logging
Browse files Browse the repository at this point in the history
allow logging of any object by inspecting non-strings
  • Loading branch information
cantino committed Mar 11, 2015
2 parents 26a8e49 + 7ee3c01 commit 0ff1c95
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 0 additions & 1 deletion app/models/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def can_control_other_agents?
end

def log(message, options = {})
puts "Agent##{id}: #{message}" unless Rails.env.test?
AgentLog.log_for_agent(self, message, options)
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/agent_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class AgentLog < ActiveRecord::Base
before_save :truncate_message

def self.log_for_agent(agent, message, options = {})
puts "Agent##{agent.id}: #{message}" unless Rails.env.test?

log = agent.logs.create! options.merge(:message => message)
if agent.logs.count > log_length
oldest_id_to_keep = agent.logs.limit(1).offset(log_length - 1).pluck("agent_logs.id")
Expand All @@ -34,6 +36,7 @@ def self.log_length

def scrub_message
if message_changed?
self.message = message.inspect unless message.is_a?(String)
self.message.scrub!{ |bytes| "<#{bytes.unpack('H*')[0]}>" }
end
true
Expand Down
5 changes: 5 additions & 0 deletions spec/models/agent_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
expect(agents(:jane_website_agent).reload.last_error_log_at.to_i).to be_within(2).of(Time.now.to_i)
end

it "accepts objects as well as strings" do
log = AgentLog.log_for_agent(agents(:jane_website_agent), events(:bob_website_agent_event).payload)
expect(log.message).to include('"title"=>"foo"')
end
end

describe "#log_length" do
Expand Down

0 comments on commit 0ff1c95

Please sign in to comment.