O'Reilly logo

Learning Rails: Live Edition by Edd Dumbill, Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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%) | 200 OK ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required