Python for Entrepreneurs Transcripts
Chapter: Monitoring and logging in production
Lecture: Logging made easy inside web action methods

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