Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to write to a custom log file from within a rake task in production mode?

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!

like image 821
Ole Spaarmann Avatar asked May 18 '10 17:05

Ole Spaarmann


People also ask

What is the default rake task in rails?

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.

Why customize rake tasks?

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.

What are some examples of out of the box rake tasks?

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


1 Answers

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"
like image 94
Daniel Beardsley Avatar answered Oct 11 '22 18:10

Daniel Beardsley