Permalink
Browse files

Add support for object inspection via awesome_print.

  • Loading branch information...
1 parent 226c705 commit f90e7274e0c6d1a4940ff4b0d17ca399f74faf9e @alistairholt alistairholt committed with cldwalker Feb 1, 2012
Showing with 33 additions and 2 deletions.
  1. +5 −0 lib/log_buddy.rb
  2. +5 −2 lib/log_buddy/utils.rb
  3. +14 −0 spec/log_buddy/log_buddy_spec.rb
  4. +9 −0 spec/log_buddy/log_spec.rb
View
@@ -33,6 +33,7 @@ module LogBuddy
def self.init(options = {})
@logger = options[:logger]
@log_to_stdout = options.has_key?(:log_to_stdout) ? options[:log_to_stdout] : true
+ @use_awesome_print = options.has_key?(:use_awesome_print) ? options[:use_awesome_print] : false
@disabled = (options[:disabled] == true)
mixin_to_object
end
@@ -55,6 +56,10 @@ def logger
def log_to_stdout?
@log_to_stdout
end
+
+ def use_awesome_print?
+ @use_awesome_print
+ end
private
@@ -43,9 +43,12 @@ def obj_to_string(obj, options = {})
"#{ obj.message } (#{ obj.class })\n" <<
(obj.backtrace || []).join("\n")
else
- obj.inspect
+ if LogBuddy.use_awesome_print?
+ obj.respond_to?(:ai) ? obj.ai : obj.inspect
+ else
+ obj.inspect
+ end
end
end
-
end
end
@@ -10,6 +10,10 @@
LogBuddy.should respond_to(:log_to_stdout?)
end
+ it "has awesome_print config option" do
+ LogBuddy.should respond_to(:use_awesome_print?)
+ end
+
it "can override the default logger" do
file_logger = Logger.new "test.log"
LogBuddy.init :logger => file_logger
@@ -26,6 +30,16 @@
LogBuddy.init :stdout => false
LogBuddy.log_to_stdout?.should == true
end
+
+ it "defaults to not using awesome_print for object inspection" do
+ LogBuddy.init
+ LogBuddy.use_awesome_print?.should == false
+ end
+
+ it "can be configured to use awesome_print for object inspection" do
+ LogBuddy.init :use_awesome_print => true
+ LogBuddy.use_awesome_print?.should == true
+ end
end
end
@@ -152,6 +152,10 @@ class Foo
def inspect
"inspeck yo-self"
end
+
+ def ai
+ "awesome_print y0"
+ end
end
it "logs string as-is" do
@@ -171,6 +175,11 @@ def inspect
it "logs all other objects with #inspect" do
obj_to_string(Foo.new).should == "inspeck yo-self"
end
+
+ it "logs object using awesome_print" do
+ LogBuddy.init :use_awesome_print => true
+ obj_to_string(Foo.new).should == "awesome_print y0"
+ end
end
describe "stdout" do

0 comments on commit f90e727

Please sign in to comment.