Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
obj_to_string for better output which depends on the type of the thin…
Browse files Browse the repository at this point in the history
…g logged
  • Loading branch information
rsanheim committed Dec 9, 2008
1 parent 241308e commit 8196370
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/log_example.rb
Expand Up @@ -108,6 +108,34 @@ def self.raise_runtime_error

end

describe "obj_to_string" do
include LogBuddy::Utils

class Foo
def inspect
"inspeck yo-self"
end
end

it "logs string as-is" do
obj_to_string("foo").should == "foo"
end

it "logs exception with exception msg, type, and backtrace" do
begin
raise "bad mojo"
rescue Exception => exception
string = obj_to_string(exception)
string.should match /^bad mojo (RuntimeError)*/
string.should include(__FILE__)
end
end

it "logs all other objects with #inspect" do
obj_to_string(Foo.new).should == "inspeck yo-self"
end
end

describe "stdout" do
before { Logger.any_instance.stubs(:debug) }
it "logs to stdout as well as the default logger" do
Expand Down
12 changes: 12 additions & 0 deletions lib/log_buddy/utils.rb
@@ -1,6 +1,18 @@
module LogBuddy
module Utils

def obj_to_string(obj)
case obj
when ::String
obj
when ::Exception
"#{ obj.message } (#{ obj.class })\n" <<
(obj.backtrace || []).join("\n")
else
obj.inspect
end
end

def debug(str)
stdout_puts(str) if log_to_stdout?
logger.debug(str)
Expand Down
5 changes: 5 additions & 0 deletions spec/mixin_spec.rb
@@ -0,0 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), *%w[helper]))

describe LogBuddy::Mixin do

end

0 comments on commit 8196370

Please sign in to comment.