Logger.Debug Not Writing to Log File in Rails

Why is Rails not writing to development.log

OK, after much going round in circles, I finally found the culprit.

The rails_12factor is apparently overwriting config.logger.

See: https://github.com/heroku/rails_stdout_logging/blob/master/lib/rails_stdout_logging/rails3.rb#L7

I removed this gem from the development environment (I really only need it in the deployed environment) and everything is now working.

To put a gem in production only, it needs to be in a gem environment group like this:

# This is override development.log output and should only go in production.
group :production do
gem 'rails_12factor'
end

Thanks @maxd for helping to get me thinking in the right direction.

Rails.logger doesn't log the data into development.log

Did you make sure the user running the Rails server has write permissions to the log file?

I've seen this happen if the server is started as root the first time

sudo rails s

which created the development.log file owned by root.
Then if a non-privileged user starts the server

rails s

the log file cannot be written to because it's owned by root.

No log messages in production.log

Assuming you're running Rails 3.2.1, this is a bug. It was patched in 3.2.2.

If you can't upgrade to 3.2.2 for any reason, this comment on GitHub has a workaround:

# config/initializers/patch_rails_production_logging.rb
Rails.logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).sync = true if Rails.logger

logger.debug vs puts in Rails debugging?

You're definitely headed the right direction in using logger.debug instead of puts as puts is more for command line applications.

Rails.logger.debug, or in short logger.debug writes the parameter text to a log file. This log file is by default environment specific. If you are currently running rails in development environment then the log file in use is log/development.log.

If you open the file log/development.log, you will find the debug text text in there. To see the log output as they are written to the file, you can use the tail command (if you are in unix system) as follows in a new terminal with present working directory as your rails application root:

$ tail -f log/development.log 


Related Topics



Leave a reply



Submit