Last active
December 27, 2015 01:59
-
-
Save diegodurs/7248662 to your computer and use it in GitHub Desktop.
Easy way to keep your debugging logs in a array + outputing to Rails.logger, useful when executing from the console
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
module ScriptUtils | |
class ArrayLogger < Array | |
def initialize(&blk) | |
@block = if block_given? | |
blk | |
else | |
->(msg){ Rails.logger.info(msg) } | |
end | |
end | |
def <<(msg) | |
@block.call(msg) | |
super(msg) | |
end | |
alias_method :write, :<< | |
def print(&blk) | |
(blk || @block).call join("\n") | |
end | |
end | |
end |
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
logs = (str || []) | |
logs.instance_eval { def <<(v); Rails.logger.info(v) && super(v); end } | |
while (doing_crasy_things) do | |
# the next line will output with Rails.logger.info + add the line to the array | |
logs << "This line failed because ... " | |
end | |
puts logs * "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment