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
0:04
it's super hard to just look at a server on the internet
0:07
that you have control over and know what's happening
0:09
without some kind of instrumentation. So we started by setting up a Logbook.
0:13
Logbook is a really nice clean way to work with logging in Python.
0:17
From the same guy who created Flask.
0:19
So we just import Logbook, and then we're going to set the level
0:24
possibly set the file name that we're going to go to
0:26
and we said look, if we're in development mode,
0:29
we want to just output our log messages to standard out
0:32
so we say logbook, stream handler, give it the standard out stream,
0:35
the level we want and push that to the application so it's available everywhere.
0:38
In production, we want to use a time rotating file handler
0:41
so basically a new log file day by day.
0:45
So we are going to do this kind instead and push that globally to the application.
0:48
After we ran this once at start up, we just create a logger
0:52
by saying the logbook logger give it a name,
0:54
that will appear as part of the hierarchy of the log,
0:57
and then you maybe come up with a message
0:59
and say like notice, info, error, warning, those kinds of things.
1:03
So the various options are log, notice, info, trace, warn, error or critical,
1:07
and depending on the level that you pass when you set up
1:11
only messages of higher priority
1:13
than the level you've sort of set as the base will come out.
1:17
So, for example, if you say I only want to work with info and above
1:20
things like trace won't appear, right, if I only want to work with notice and above,
1:25
info and trace won't appear in my log, so you can dial that in a little bit there.
1:28
Here's what you get when you do one of these log messages,
1:32
date, time, the level, here we have notice again,
1:36
the app which is the name that we put at the top
1:38
and then the message we actually wanted to give them.
1:41
So that's great for the logs, but how often do you go back and read those, right,
1:46
especially if you have a super busy site, it could be millions of lines of
1:49
the users viewed this page, the users viewed that page,
1:52
somebody is trying to hack your WP admin page,
1:55
which you don't have, things like this.
1:57
But you want to get notifications if something goes wrong,
2:00
if your database goes down or there's some primary key violation that you never expected,
2:04
you would like to not know eventually when your user complaints to you,
2:07
you would like a notification immediately.
2:10
So we also installed Rollbar, you can get a free account there,
2:14
there's also a paid account, but the free account is pretty decent here
2:17
and all you have to do is add a little bit of configuration
2:19
to your either development or production.any file
2:23
and then just a tiny bit of code to include it,
2:27
and use it in your __init__for your entry point
2:30
and that's it, everything's up and running, it just basically intercept all the requests
2:35
to your site, and if there's anything that goes wrong, it'll capture all that information,
2:41
store it for you and send you some kind of notification.
2:43
So, you can check out how to set that up here.