Last active
January 9, 2018 23:41
-
-
Save dukejones/7028c9238104ad92c7471e9127346cf1 to your computer and use it in GitHub Desktop.
Use different rails log for rake tasks
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
# Use like: `task mytask: :setup_logger do` | |
# Rather than: `task mytask: :environment do` | |
task setup_logger: :environment do | |
set_logger(file: Rails.root.join('log', 'rake.log')) | |
end | |
# Or just call set_logger(file: 'custom_file.log') from your rake task. Be sure to use an absolute path. | |
def set_logger(file: STDOUT, level: Logger::INFO) | |
logger = ActiveSupport::TaggedLogging.new(Logger.new(file)) | |
logger.level = level | |
logger.formatter = Rails.logger.formatter | |
Rails.logger = logger | |
end | |
# Technically, it's better to just log to STDOUT 12-factor style. | |
# Change ActiveRecord::Base.logger to change where the queries are logged. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment