Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the Rails logger from an initializer?

Following the advice from my previous question, I placed my background process in an initializer named scheduler.rb. However, I'm having a hard time getting the newly-scheduled processes to log to the Rails logs. Is there a simple way for me to access the same logs from the initializer, preferably by accessing Rails' default logger methods (logger.info, etc)?

like image 641
abeger Avatar asked Jan 22 '10 22:01

abeger


People also ask

How do I view a Rails logger?

If you want to know the current log level, you can call the Rails. logger. level method. This is useful when you want to log under development or staging without flooding your production log with unnecessary information.

Where does Rails logger info write to?

By default it puts these log files in the log/ directory of your project. So if you open up that folder you'll see a development.

What is Initializers in Ruby on Rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.


2 Answers

Rails 3-
Simply use Rails.logger in your initializer

Rails.logger.info "blabla"

HTH

like image 138
Hertzel Guinness Avatar answered Oct 07 '22 18:10

Hertzel Guinness


RAILS_DEFAULT_LOGGER was deprecated in Rails 3. The following steps work for me in Rails 3.1.

Set your logger in environment.rb before calling initialize! on your application:

Rails.logger = Logger.new(STDOUT)
MyServer::Application.initialize!

Then call the logger in your initializer.

Rails.logger.info "Hello, world!"
like image 22
Jason Avatar answered Oct 07 '22 18:10

Jason