Paul
Paul

Reputation: 26660

Make consistent Rails logging

How to make Rails logging work equally in all environments?

When I use both, it makes problems in Apache/Passenger environment because all the console output is being logged to log file and I get double lines for each logging event.

Upvotes: 0

Views: 79

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176412

You should not use puts for real logging. It maybe useful for debugging, but it's not a proper logging mechanism.

If you want to log something, then the correct way is to use Rails.logger.

Rails.logger.info "something"

By default, the logger logs in the log files only. Some servers, such as Passenger, were used to also output the logs in the console but this is not the standard behavior.

If you want to log also in the console, you can set the logger to behave differently (for instance) in the development environment. Simply create a new logger with the custom actions.

The Rails logger is an ActiveSupport::Logger and it inherits from Ruby Logger

Example, to log in the console in development assign the following logger in the development environment

Rails.logger = ActiveSupport::Logger.new(STDOUT)

If you want to log both to files and console, create a custom logger class that proxies every logger. call to two instances, one the file logger and the other the console logger.

However, the simplest think is to tail -f the development log in another console tab to get access to the log entries.

Upvotes: 1

Related Questions