Created
March 7, 2011 05:04
-
-
Save zeero/858099 to your computer and use it in GitHub Desktop.
Logger.debug_var
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
class Logger | |
def debug_var(ctx, *vars) | |
# get Class name & decide method type | |
class_name = ctx.eval("self.class").to_s | |
if class_name != "Class" | |
sep = "#" | |
else | |
class_name = ctx.eval("self.name") | |
sep = "." | |
end | |
# get method name | |
/^(.+?):(\d+)(?::in `(.*)')?/.match(caller.first) | |
method_name = $3 ? "#{$3}" : "" | |
# create debug message as Array | |
message_a = vars.map {|var| "#{var} => #{ctx.eval(var.to_s).inspect}" } | |
debug(class_name + sep + method_name) { message_a.join(", ") } | |
end | |
end | |
class Foo | |
def foo(str) | |
log = Logger.new($stdout) | |
log.datetime_format = "%Y-%m-%d %H:%M:%S" | |
#log.level = Logger::WARN unless $DEBUG | |
log.debug_var binding, :str | |
end | |
def self.bar(str1, str2) | |
log = Logger.new($stdout) | |
log.debug_var binding, :str1, :str2 | |
end | |
end | |
Foo.new.foo("test") | |
Foo.bar("static", "multi_args") | |
# >> D, [2011-03-07 22:02:28#68012] DEBUG -- Foo#foo: str => "test" | |
# >> D, [2011-03-07T22:02:28.762433 #68012] DEBUG -- Foo.bar: str1 => "static", str2 => "multi_args" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment