On Rails irb Logging
June 26, 2011 § Leave a comment
When dealing with Rails apps logging debug statements is one of first methods turned to when trying to diagnose a problem. A lot of times
logger.debug is used and will write its output to
log/development.log. This is great when you are running the server, but when you fire up the console and want to work there, you previously had two options. One is to change all of the
logger.debug statements into
puts so they show up in the console’s terminal window. The other is two just keep switching back and forth between the console and a tail of the file.
Now you can toggle the app’s logger so that it writes to
STDOUT. Just throw this in your
.irbrc file and call
toggle_console_logging from the console’s command line.
def toggle_console_logging if ActiveRecord::Base.logger == Rails.logger l = Logger.new(STDOUT) l.level = Rails.logger.level set_logger l and return "console" else set_logger Rails.logger and return "log file" end end def set_logger(logger) ActiveRecord::Base.logger = logger ActiveRecord::Base.clear_active_connections! end
You can check out the gist here.
Leave a Reply