RAILS_DEFAULT_LOGGER: Writing to the Log in Rails

Posted about 16 years ago

One great thing about Rails is the logs that are produced in development mode. By default rails will list out all SQL statements and trace all controller calls.

It is possible to add custom messages to the rails log through a poorly documented RAILS_DEFAULT_LOGGER object which is defined in environment.rb.

The following log levels are available:

  • debug
  • info
  • warn
  • error
  • fatal

Therefore, to add debug calls to your code for development mode just add the following:

RAILS_DEFAULT_LOGGER.info "foo bar"

I've found it useful to add a method to application_helper for this for consistency. It helps as well to add line feeds and searchable text to make your output stand out in the log file.

def add_to_log(output)
  RAILS_DEFAULT_LOGGER.info "\n ***mylog*** #{output}\n"
end