Logging

You may not have thought of it this way, but you’ve been working with Rails logs since the first time you entered script/server. All of that information flowing by is the development log. You can find all of it in the log directory of your application, stored in the development.log file. (There are also test.log and production.log files there for use when your application runs in test or development mode, as described in the next chapter.)

While Rails is certainly generous with the information that it sends to the log in development mode, that sheer volume can make it hard to find things. It may also not be sending what you want to see. If you want to send something specific to the log, use the logger object in your model, controller, or view. In a model or controller, this would look like:

logger.info 'This is a message to send to the log'

while in the view it would look like:

<% logger.info 'This is a message to send to the log' %>

You can use <%= rather than <% to send the message to both the screen and the logger if you want to combine a visible message with a permanent record:

<%= logger.info 'This is a message for the view and the log' %>

The user would then see “This is a message for the view and the log” on her screen, and it would also be stored in the log file.

One piece of information that is logged and is worth pointing out is timing information. You’ll find lines in the log like:

Completed in 0.01451 (68 reqs/sec) | Rendering: 0.00775 (53%) | DB: 0.00093 (6%) ...

Get Learning Rails: Live Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.