I'm trying to write to my log files while running a rake task. It works fine in development mode, but as soon as I switch to the production environment, nothing is written to the log files.
I read here
How do I use a custom log for my rake tasks in Ruby on Rails?
that this is the normal behavior and also found a #wontfix ticket in lighthouse.
My question: Is there a way to output, what's going on while my rake task is running? It performs some crawling and runs for hours. I would prefer if the output went in a specific log file like /log/crawler.log
Right now I'm just using this command to write to the log files:
ActiveRecord::Base.logger.info "Log Text"
Thanks!
The default rake task can be configured to do whatever your application needs. In the next section, I will go through my usual set up for Rails applications. The configuration I like to use overrides both the default and the test rake tasks, integrating them with a few tools.
Customizing rake tasks to fit your needs can improve your productivity and help to guarantee that everyone in the project is using a standardized process (or at least that they have the tools to easily do so). How to do it? Overriding rake tasks is pretty straight forward.
Here are a few examples of out of the box tasks: $ rake -T # Lists all rake tasks for the application $ rake # Default rake task. On a fresh application, does the same as rake test $ rake test # Task to run your tests (replaced by "rake spec" if using rspec instead of minitest) $ rake db:seed # Populates the database using the seeds.rb file
The problem you are having is that rails ignores 'info' level logs in production mode.
I'd recommend reading this: http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html
and creating your own logger:
logger = Logger.new('logfile.log')
logger.info "Something happened"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With