#100DaysOfCode in Python Transcripts
Chapter: Days 31-33: Logging
Lecture: Concepts: Logging

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that you've seen logging and logbook in action, let's quickly review some of the concepts. So, we're going to start by importing the logbook,
0:09 And if we're going to not use a file, we're going to need to also import sys, so we can get to standard out. Then we're going to maybe get a file name,
0:18 that can come from, like a configuration file, the environment, you could hard-code it, whatever you want. We're going to set a logging level here.
0:27 Now I didn't actually demonstrate it, but if we went and switched that to say warnings, you would no longer see any of the messages that were trace,
0:35 right? Only the stuff warning and above would appear in the log file, or in the console, and nothing else. So you can dial this up and down,
0:42 depending on whether you're running a debug build, or production build, or release build, things like that.
0:48 So, set this, and that may also be based on configuration, or version, type of app, right? Like it's production or development and so on.
0:57 Then we have to install a handler, so we're either going to use a stream handler, set that to standard out,
1:02 set the level of push it to the application. Or, if you want to go to a file, I recommend the timed rotating file handler, and do the same thing.
1:11 Once this is set up, then it's basically ready to go. So remember this level acts as a filter here, so you can always go logger.trace, logger.info,
1:23 but only if the level is low enough, will it actually show on these handlers. Now, you do that, what we just saw, once,
1:33 at the beginning of your app, and then, anytime you want to log something, you're going to create one of these loggers, like, heres a start up logger.
1:40 And we have this little info message, like, hey, we're getting started like this. Now this just a string, nothing logging about it.
1:46 But then we can go to our startup log and say notice, this message, right? And that's a pretty high level of log message there, this notice level.
1:56 And this is going to go, probably near the front, so it'll tell you what level you're logging at, which will help you understand what
2:02 the rest of the log means. We have different options, we have notice, info, trace, warn, error, and critical.
2:09 So critical is even more of an error than error, right? We also have exception, if you just want to show a straight up exception.
2:16 And then the output looks like this, it has the time, it has the level, here, notice, it has the log category, log name, in this case, app,
2:24 and then it finally has the actual message. Really nice, really easy to set up, and it's super, super flexible.


Talk Python's Mastodon Michael Kennedy's Mastodon