Start a free Courses trial
to watch this video
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.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[treehouse presents] 0:00 [How to Use the Logger in Ruby on Rails with Jason Seifer] 0:02 Hi, I'm Jason. 0:05 In this Treehouse quick tip, 0:07 we're going to talk about how to log in Ruby on Rails. 0:09 Logging is an extremely useful tool that's build into every Rails application. 0:11 By default, a Rails app will log the URL of the current request, 0:17 the parameters, database calls, 0:21 and a few other useful pieces of information. 0:23 It's possible to use your own log statements in your applications, 0:26 and it's also very easy to do. 0:30 There are 5 different levels of logging in Ruby on Rails, 0:33 debug, info, warn, error, and fatal. 0:36 The defaults vary per environment as well. 0:41 In production, all logging is set to info level by default. 0:44 Every other environment is set to debug. 0:49 What this means is that you won't see as many log messages 0:52 coming through your production logs. 0:55 In development and test environments, 0:57 you probably want to see more information, 0:59 which is why it's set to debug. 1:01 Now let's take a look at how to add a debug statement to a Rails application. 1:05 I've got a sample Rails application right here 1:09 that I'm going to start the server of right now. 1:11 So now with this application started, 1:17 I can go and take a look at what's going on here. 1:19 If I go to localhost:3000, 1:23 you can see I have a random listing of status updates. 1:25 This is a sample application called TreeBook, 1:28 which simulates a social network. 1:31 So if I go look at the log here, 1:33 you can see that Rails prints our some information for us 1:37 saying what controller we're on 1:40 as well as some database calls 1:42 and what's going on here from the cache 1:44 and what it's rendering and what it's returning. 1:47 So let me clear this for a second. 1:49 If I want to add my own debug statement, 1:52 I can do that right here in the controller 1:54 or even in one of the models. 1:57 Right now I'm going to add a logging statement for debugging. 1:59 So I'm going to say logger.debug, 2:02 and I'm just going to put something in all caps hereβ 2:05 "I AM A DEBUGGING STATEMENT" 2:11 So I save that file, and I go back to my application. 2:13 Now I reload it in my web browser. 2:16 And then if I go check the log back here, 2:19 you can see that it prints I AM A DEBUGGING STATEMENT, 2:22 which is the same thing that I wrote here in my controller. 2:26 Now if I wanted to, I could also have written this 2:30 inside one of the models or views 2:33 although you don't generally log things from views. 2:35 So this isn't very useful except for maybe saying where I am in the application. 2:39 The more useful thing to do would be to debug the statuses. 2:44 So I could do something like logger.debug 2:48 and inspect the different status in my log. 2:54 Now let me clear this 2:58 and reload the page. 3:00 If I go back to the log here, 3:02 I can see the different inspections of the status model right here in my log. 3:05 This could be really useful when you're trying to debug certain parts of an application. 3:12 Now if I were to restart this in production mode, 3:17 the production log wouldn't display anything 3:21 because it's going to a log level of debug. 3:24 You can change what level of debugging is going on 3:27 inside of the different environment files. 3:31 And over here in production, you would uncomment this line 3:35 and change the log level to info or warn or fetal. 3:40 As you can see, it's extremely easy to add logging statements to your application. 3:46 Next time you get caught up and can't figure out 3:51 why your app is behaving a certain way, 3:53 try looking at the logs and checking out the status of your objects. 3:55
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up