Created
July 11, 2016 12:58
-
-
Save jameslafa/c03f09784b0a24531be43dd9250d2fd5 to your computer and use it in GitHub Desktop.
Easily debug rake task
This file contains 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
desc "switch rails logger to stdout" | |
task :verbose => [:environment] do | |
Rails.logger = Logger.new(STDOUT) | |
end | |
desc "switch rails logger log level to debug" | |
task :debug => [:environment, :verbose] do | |
Rails.logger.level = Logger::DEBUG | |
end | |
desc "switch rails logger log level to info" | |
task :info => [:environment, :verbose] do | |
Rails.logger.level = Logger::INFO | |
end |
On the initialization, Rails stores a logger instance reference in Rails.logger
and also in some other places, like ActiveRecord::Base.logger
, ActiveJob::Base.logger
, etc. It is the same object referenced in different locations.
Rails.logger == ActiveRecord::Base.logger # => true
Better than create a new logger like in line 3 of debug.rake
would be change the existing object output to STDOUT
. It is possible to accomplish that using the logger reopen
method. Like this:
Rails.logger.reopen(STDOUT)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool