Python for Entrepreneurs Transcripts
Chapter: Monitoring and logging in production
Lecture: Logging made easy inside web action methods
0:02 Now that we have Logbook all set up, and loading at startup, let's see how we can leverage our controller infrastructure that we've set up,
0:11 basically our Pyramid handlers, to make it really easy to get specific messages
0:16 for any web action in our entire website without even thinking about it. So here is some that we might want to log, sign in,
0:24 that seems like something that you want to know about, people register, people sign in, these are really good things to log;
0:30 similarly when somebody purchases something, we want to log that as well. So we could go create a Logbook, enter some information,
0:37 but you'll see this can actually be done automatically leveraging our base controller.
0:40 So down here, let's just say self.log and let me put a little log_name here, remember, this is going to basically be the section of our application,
0:55 so we could say first it's going to be just controllers in general
0:58 and we can start with this, we'll say logbook, we got to import that into this module,
1:04 and then we'll create a logger and we give it the log name. Super simple. So up here, like in "sign in", here, let's just do a little message,
1:15 and let's do self.log.notice, failed login, something like that. And we'll do the message here and then down here after this, we'll say
1:38 "self.log.info..." and notice as well I guess, "User successfully logged in", and let's go ahead and say "vm.email", so we know who that was.
1:53 Alright, let's check this out see what's working. Let me log out, and then sign in, and I think I changed to the "cat" at some point,
2:05 so let's try "cat", boom, we are logged in, let's check the logs, user successfully logged in. Let me just try a fail real quick, no that didn't work,
2:17 so I must have messed up my "cat", there we go. So we have notice, controllers, failed login attempt, and so that's cool,
2:25 these notices are coming from app, these notices are coming from controller, but, one simple step and we can do better, alright.
2:32 So that was already cool that now by doing this setup, every web action has instant access to our logging infrastructure,
2:40 but let's do a little bit of work on this name here. What we can do is actually we can shorten this to say this is going to be a controller,
2:49 say just "Ctrl" and I am going to use a short version, just so the log file lines don't get too long "/" and then we could say
2:58 "Account" or "Store" or whatever to say which controller, how do we do that? We can actually leverage the type name of the instance,
3:10 we are going to say type (self).__name__ and this would be something like the full path name or at least account controller
3:20 so what we want to do is we are going to actually just replace "Controller" with nothing.
3:29 So instead of saying controller/account controller we'll just take controls,
3:35 say controllers maybe plural/account/admin/store, you don't have to do this way,
3:43 you could just stuff straight name into there but I kind of like the grouping of this
3:47 so if you do some kind of like sorting on the name or the location, it's all the controllers are grouped together. Alright, let's try one more time.
3:58 Alright, log out, sign in, now we go down here, we can see oh Ctrls/Account, user Michael logged in,
4:09 so now we know exactly what controller or web action we were in, more or less, we can do one final step here,
4:16 now let's go ahead and log that somebody purchased something here. So we'll say "YAY, we have a purchase, somebody bought something",
4:24 so notice that PyCharm is showing us that we are missing some pieces so we say self.logged_in_user.email, what did they buy,
4:33 they bought album.name. OK, let's go buy that. So we'll come over here, let's log out really quick,
4:46 log back in, take it through the whole process. Alright, now we are logged in, we can go to albums and hey, we can buy this album, and remember,
4:56 our test codes four twos all the way across, alright, let's buy it. Success, now let's check out logs, OK, so we've come down here,
5:14 the user has logged in, we have some request going to Stripe etc, etc, etc, I think I am printing that out somewhere, we could take that away
5:27 and then we have Ctrls/Store, yeey, Michael bought Digital Age Boys And Girls. OK, so besides that print statement there, which I can get rid of,
5:37 you can see we have Ctrls/Account, Ctrls/Store and so on. So now, with just this little bit of work, we are going to have a really good logging
5:48 for the rest of our application. Right, that's Logbook, I totally recommend you use it,
5:53 I've been using it for long time and it's really stable and easy to use, and it makes logging fun.