Skip to content

Instantly share code, notes, and snippets.

@diegodurs
Last active December 27, 2015 01:59
Show Gist options
  • Save diegodurs/7248662 to your computer and use it in GitHub Desktop.
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
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
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