views:

1047

answers:

4

I have a rake task that calls functions like this:

namespace :blah do
    task :hello_world => :environment do
       logger.info("Hello World")
       helloworld2
    end
end

def helloworld2
   logger.info("Hello Again, World")
end

I want the log output to a custom log, and I really don't want to have to pass a log reference every time I make a function call. I found this somewhere (can't find it again):

def logger
  @@logger ||= Logger.new("#{RAILS_HOME}/log/blah.log")
end

But this does not work for me and I am not sure what it even does because I grabbed the code a long time ago and haven't used it until now. I can't search for @@ on google (tried +"@@" rails) to see what it does. Any help on this issue would be great. I am hoping for a quick solution and not having to install a gem or plugin (unless there is a really really good reason to.

Thanks!

A: 
  1. What do you mean by "does not work for me"? I just tried this same code and it worked - created a new log file and put some text in it.
  2. @@logger is a class variable, it's a language issue, not Rails' one. I believe there's no need in further explanations :)
  3. You've probably mistaken typing "function helloworld2" :)
neutrino
function was a mistake. maybe it's a path issue. when i say doesn't work, i mean nothing gets written to the log file. i have a "puts 'hello world'" and a "logger.info('hello world')" in consecutive lines. I see the puts, but not the logger
Tony
Ah, put RAILS_ROOT instead of RAILS_HOME
neutrino
strange...this still is not working and the path is definitely right. this EXACT code works for you?
Tony
Except that I have RAILS_ROOT, not RAILS_HOME. I created a new .rake file in my lib/tasks folder, the contents are below. I'm running it with Rails 2.3.2, and both strings are properly output. Honestly, I have no idea why this can't work.namespace :blah do task :hello_world => :environment do logger.info("Hello World") helloworld2 endenddef helloworld2 logger.info("Hello Again, World")enddef logger @@logger ||= Logger.new("#{RAILS_ROOT}/log/blah.log")end
neutrino
try that with 2 different tasks and 2 different log files. i think that is where my problem lies. the first task i created writes to its log file fine but then the second task i created writes to the first task's log file.
Tony
A: 

Advanced Rails Recipes Recipe 84 from @topfunky shows how to define a custom logger. He has some code in the environment config file (production would look like this): RAILS_ROOT/config/environments/production.rb:

config.logger = RAILS_DEFAULT_LOGGER = Logger.new(config.log_path)

I'd test that out instead of redefining the class variable as you have. He might have something on http://nubyonrails.com to check as well.

Andy Atkinson
where do you define config.log_path? how is config.logger defined? why are you setting it to RAILS_DEFAULT_LOGGER? Thanks
Tony
+2  A: 

rake disables logging in production mode. make sure you're running in development mode if you want it to log

David
+1  A: 

This is the best solution I have been able to come up with: http://www.tonyamoyal.com/2009/09/24/specific-logging-for-your-rails-models-the-easy-way/

Tony