views:

817

answers:

3

In a rake task if I use puts command then I see the output on console. However I will not see that message in log file when app is deployed on production.

However if I say Rails.logger.info then in development mode I see nothing on console. I need to go to log file and tail that.

I would ideally like to use Rails.logger.info and in development mode inside the rake task, the output from logger should also be sent to console.

Is there a way to achieve that?

A: 

I'd say that using Rails.logger.info is the way to go.

You won't be able to see it in the server console because it won't run via the server. Just open up a new console and tail -f the log file, it'll do the trick.

Many users are aware of the UNIX® command 'tail', which can be used to display the last few lines of a large file. This can be useful for viewing log files, etc.

Even more useful in some situations, is the '-f' parameter to the 'tail' command. This causes tail to 'follow' the output of the file. Initially, the response will be the same as for 'tail' on its own - the last few lines of the file will be displayed. However, the command does not return to the prompt, and instead, continues to 'follow' the file. When additional lines are added to the file, they will be displayed on the terminal. This is very useful for watching log files, or any other file which may be appended over time. Type 'man tail' for more details on this and other tail options.

(via)

marcgg
But it's not very comfortable while working on the local machine as you don't see the output in the same window where you called the task. Especially if you don't have a big screen to fit multiple windows side by side.
Tomas Markauskas
Even better is to use `tailf` "It is similar to tail -f but does not access the file when it is not growing" (from manpage). It is shorter too
MBO
@tomas why not minimizing the server log console and only have the one console with the tail-f running ? Anyways it's not a real problem... I am running like 8 consoles tracing what's going on in my app, just switch between tabs when you're working on a specific part of the system no big deal imho
marcgg
@mbo nice :) halas it doesn't look like it's available on my machine (mac os@leopard)
marcgg
@marcgg: I don't have a "server console" (I use passenger), but the question is about rake tasks and if you run a task from one terminal window, you don't see anything from the logger in that window. You have to have another window with the development.log to see the output. Ideally I would somehow add stdout as another output stream to Rails.logger, but not remove the original one.
Tomas Markauskas
@tomas you could try building your own logger: http://ruby-doc.org/core/classes/Logger.html
marcgg
I can build my own logger. But the goal was to messages from rake go to both rails default logger and to the console. I was wondering if I was missing something. It seems there it is not trivial to also redirect logger to do puts.
Nadal
+1  A: 

Rake tasks are run by a user, on a command-line. Anything they need to know right away ("processed 5 rows") should be output on the terminal with puts.

Anything that needs to be kept for posterity ("sent warning email to [email protected]") should be sent to the Rails.logger.

Jonathan Julian
+1  A: 

How about creating an application helper which detects which environment is running and does the right thing?

def output_debug(info)
   if RAILS_ENV == "development"
      puts info
   else
      logger.info info
   end
end

Then call output_debug instead of puts or logger.info

naven87