Python for Entrepreneurs Transcripts
Chapter: Course Conclusion
Lecture: Lightning review: Monitoring
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
We don't really want to run our code without knowing what's going on it's super hard to just look at a server on the internet
0:08
that you have control over and know what's happening without some kind of instrumentation. So we started by setting up a Logbook.
0:14
Logbook is a really nice clean way to work with logging in Python. From the same guy who created Flask.
0:20
So we just import Logbook, and then we're going to set the level possibly set the file name that we're going to go to
0:27
and we said look, if we're in development mode, we want to just output our log messages to standard out
0:33
so we say logbook, stream handler, give it the standard out stream, the level we want and push that to the application so it's available everywhere.
0:39
In production, we want to use a time rotating file handler so basically a new log file day by day.
0:46
So we are going to do this kind instead and push that globally to the application. After we ran this once at start up, we just create a logger
0:53
by saying the logbook logger give it a name, that will appear as part of the hierarchy of the log, and then you maybe come up with a message
1:00
and say like notice, info, error, warning, those kinds of things. So the various options are log, notice, info, trace, warn, error or critical,
1:08
and depending on the level that you pass when you set up only messages of higher priority than the level you've sort of set as the base will come out.
1:18
So, for example, if you say I only want to work with info and above
1:21
things like trace won't appear, right, if I only want to work with notice and above,
1:26
info and trace won't appear in my log, so you can dial that in a little bit there. Here's what you get when you do one of these log messages,
1:33
date, time, the level, here we have notice again, the app which is the name that we put at the top
1:39
and then the message we actually wanted to give them. So that's great for the logs, but how often do you go back and read those, right,
1:47
especially if you have a super busy site, it could be millions of lines of the users viewed this page, the users viewed that page,
1:53
somebody is trying to hack your WP admin page, which you don't have, things like this. But you want to get notifications if something goes wrong,
2:01
if your database goes down or there's some primary key violation that you never expected,
2:05
you would like to not know eventually when your user complaints to you, you would like a notification immediately.
2:11
So we also installed Rollbar, you can get a free account there, there's also a paid account, but the free account is pretty decent here
2:18
and all you have to do is add a little bit of configuration to your either development or production.any file
2:24
and then just a tiny bit of code to include it, and use it in your __init__for your entry point
2:31
and that's it, everything's up and running, it just basically intercept all the requests
2:36
to your site, and if there's anything that goes wrong, it'll capture all that information, store it for you and send you some kind of notification.
2:44
So, you can check out how to set that up here.