Logging is extremely useful. URL, paramaters, database calls, and other useful information. There are 5 levels of logging in Ruby on Rails: Debug, Info, Warn, Error, and Fatal. In production, all logging is set to “Info” level by default. All other levels are set to “Debug”

This video will show you how to add a debug statement to a Rails application. In the log file Rails shows the controller, database calls, the cache, what it’s rendering, and what it is returning. You can add a debug logging statement in the controller using logger.debug command.

It’s easy to add logging statements to your web apps. If your app is returning errors, try checking the logs to see the status of your objects.

Video Transcription

Jason: Hi, I’m Jason. In this Treehouse quick tip, we’re going to talk
about how to log in Ruby on Rails. Logging is an extremely useful tool
that’s built in to every Rails application. By default, a Rails applet
logged the URL of the current request, the parameters, database calls, and
a few other useful pieces of information. It’s possible to use your own log
statements in your applications, and it’s also very easy to do.

There are five different levels of logging in Ruby on Rails: Debug, Info,
Warn, Error, and Fatal. The defaults vary per environment as well. In
production, all logging is set to Info level by default. Every other
environment is set to Debug. What this means is that you won’t see as many
log messages coming through your production logs. In development and test
environments, you probably want to see more information, which is why it’s
set to Debug.

Now let’s take a look at how to add a Debug statement to a Rails
application. I’ve got a sample Rails application right here that I’m going
to start the server off right now. Now with this application started, I can
go and take a look at what’s going on here. If I go to local host 3,000,
you can see I have a random listing of status updates. This is a sample
application called Treebook, which simulates a social network. If I go look
at the log here, you can see the Rails print out some information for us,
saying what controller we’re on, as well as some database calls, and what’s
going on here from the cache, and what it’s rendering and what it’s
returning. Let me clear this for a second.

If I want to add my own Debug statement, I can do that right here in the
controller, or even in one of the models. Right now, I’m going to add a
logging statement for debugging, so I’m going to say “logger.debug”, and
I’m just going to put something in all caps here, “I’m a debugging
statement.” So I save that file and then I go back to my application. Now I
reload it in my web browser, and then, if I go check the log back here, you
can see that it prints, “I am a debugging statement,” which is the same
thing that I wrote here in my controller. Now, if I wanted to, I could also
have written this inside one of the models or views, although you don’t
generally log things from views. This isn’t very useful except for maybe,
saying where I am in the application.

A more useful thing to do would be to debug the statuses. I can do
something like “logger.debug” and inspect the different statuses in my log.
Let me clear this, and reload the page. If I go back to the log here, I
can see the different inspections of the status model, right here in my
log. This can be really useful when you’re trying to debug certain parts of
an application.

Now, if I were to restart this in production mode, the production log
wouldn’t display anything because it’s going to a log level of Debug. You
can change what level of debugging is going on inside of the different
environment files. Over here, in production, you would uncomment this line
and change the log level to Info or Warn or Fatal. As you can see, it’s
extremely easy to add logging statements to your application. Next time you
get caught up and can’t figure out why your app is behaving a certain way,
try looking at the logs and checking out the status of your objects.

Announcer: If you’d like to see more advanced videos and tutorials like
this one, go to TeamTreehouse and start learning for free.